Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Hackerrank.NewYearChaos
  6. {
  7. class Program
  8. {
  9. private static int _amountCases;
  10.  
  11. private static int _currentCase = 0;
  12. private static int _currentQueueLength;
  13.  
  14.  
  15.  
  16. static void minimumBribes(int[] q)
  17. {
  18. _amountCases = q[0];
  19. _currentQueueLength = q[1];
  20.  
  21. List<int> currentQueueArray = CreateQueueArray(q);
  22.  
  23. for (int selectedCase = 0; selectedCase < _amountCases; selectedCase++)
  24. {
  25. bool wasTooChaotic = false;
  26. int totalDifference = 0;
  27. for (int i = 0; i < _currentQueueLength; i++)
  28. {
  29. int difference = currentQueueArray[i] - (i + 1);
  30. if (difference > 2)
  31. {
  32. Console.WriteLine("Too chaotic");
  33. wasTooChaotic = true;
  34. break;
  35. } else
  36. {
  37. totalDifference += difference;
  38. currentQueueArray.Insert(i+1+difference, currentQueueArray[i]);
  39. currentQueueArray.RemoveAt(i);
  40.  
  41. };
  42. }
  43. if (!wasTooChaotic)
  44. {
  45. Console.WriteLine(totalDifference);
  46. }
  47. IterateNextCurrentCaseAndQueueLength(q);
  48. currentQueueArray = CreateQueueArray(q);
  49. }
  50.  
  51. }
  52.  
  53. private static List<int> CreateQueueArray(int[] q)
  54. {
  55. var currentQueue = new List<int>();
  56. for (int i = 0; i < _currentQueueLength; i++)
  57. {
  58. int selectQIndex = _currentCase + 2 + i;
  59. currentQueue.Add(q[selectQIndex]);
  60. };
  61.  
  62. return currentQueue;
  63. }
  64.  
  65. public static int IterateNextCurrentCaseAndQueueLength(int[] q)
  66. {
  67.  
  68. _currentCase += 2;
  69. _currentQueueLength = q[_currentCase];
  70. return _currentQueueLength;
  71. }
  72.  
  73. static void Main(string[] args)
  74. {
  75. int[] q = new int[] { 2, 5, 2, 1, 5, 3, 4, 5, 2, 5, 1, 3, 4 };
  76. minimumBribes(q);
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement