Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. // Complete the minimumBribes function below.
  2. static void minimumBribes(int[] q) {
  3. int amountOfNumbers = q.Length;
  4. Dictionary<int, int> numberSwapCounter = new Dictionary<int, int>();
  5.  
  6. int amountOfSwaps = 0;
  7. for (int i = 0; i < amountOfNumbers; i++)
  8. {
  9. for(int j = 0; j < amountOfNumbers - 1; j++)
  10. {
  11. int left = q[j];
  12. int right = q[j + 1];
  13. if (left > right)
  14. {
  15. numberSwapCounter.TryGetValue(left, out var amountNumberSwapped);
  16. numberSwapCounter[left] = ++amountNumberSwapped;
  17. if (amountNumberSwapped > 2)
  18. {
  19. Console.WriteLine("Too chaotic");
  20. return;
  21. }
  22. q[j] = right;
  23. q[j + 1] = left;
  24. amountOfSwaps++;
  25. }
  26. }
  27. }
  28. Console.WriteLine(amountOfSwaps);
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement