Advertisement
Guest User

Untitled

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