Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace Hackerrank.NewYearChaos
- {
- class Program
- {
- private static int _amountCases;
- private static int _currentCase = 0;
- private static int _currentQueueLength;
- static void minimumBribes(int[] q)
- {
- _amountCases = q[0];
- _currentQueueLength = q[1];
- List<int> currentQueueArray = CreateQueueArray(q);
- for (int selectedCase = 0; selectedCase < _amountCases; selectedCase++)
- {
- bool wasTooChaotic = false;
- int totalDifference = 0;
- for (int i = 0; i < _currentQueueLength; i++)
- {
- int difference = currentQueueArray[i] - (i + 1);
- if (difference > 2)
- {
- Console.WriteLine("Too chaotic");
- wasTooChaotic = true;
- break;
- } else
- {
- totalDifference += difference;
- currentQueueArray.Insert(i+1+difference, currentQueueArray[i]);
- currentQueueArray.RemoveAt(i);
- };
- }
- if (!wasTooChaotic)
- {
- Console.WriteLine(totalDifference);
- }
- IterateNextCurrentCaseAndQueueLength(q);
- currentQueueArray = CreateQueueArray(q);
- }
- }
- private static List<int> CreateQueueArray(int[] q)
- {
- var currentQueue = new List<int>();
- for (int i = 0; i < _currentQueueLength; i++)
- {
- int selectQIndex = _currentCase + 2 + i;
- currentQueue.Add(q[selectQIndex]);
- };
- return currentQueue;
- }
- public static int IterateNextCurrentCaseAndQueueLength(int[] q)
- {
- _currentCase += 2;
- _currentQueueLength = q[_currentCase];
- return _currentQueueLength;
- }
- static void Main(string[] args)
- {
- int[] q = new int[] { 2, 5, 2, 1, 5, 3, 4, 5, 2, 5, 1, 3, 4 };
- minimumBribes(q);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement