Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Complete the minimumBribes function below.
- static void minimumBribes(int[] q) {
- int amountOfNumbers = q.Length;
- Dictionary<int, int> numberSwapCounter = new Dictionary<int, int>();
- int amountOfSwaps = 0;
- for (int i = 0; i < amountOfNumbers; i++)
- {
- for(int j = 0; j < amountOfNumbers - 1; j++)
- {
- int left = q[j];
- int right = q[j + 1];
- if (left > right)
- {
- numberSwapCounter.TryGetValue(left, out var amountNumberSwapped);
- numberSwapCounter[left] = ++amountNumberSwapped;
- if (amountNumberSwapped > 2)
- {
- Console.WriteLine("Too chaotic");
- return;
- }
- q[j] = right;
- q[j + 1] = left;
- amountOfSwaps++;
- }
- }
- }
- Console.WriteLine(amountOfSwaps);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement