You are viewing a single comment's thread. Return to all comments →
This works (y).
public static void minimumBribes(List q) { var arr = q.ToArray(); int totalpositionMoved = 0; bool isChaotic = false; //
Dictionary<int, int> map = new Dictionary<int, int>(); for (int i = 0; i < q.Count; i++) { map.Add(q[i], i); } // for (int sticker = arr.Length ; sticker > 0; sticker --) { int currentposition = map[sticker]; if (currentposition < 0) { break; } int actualPosition = sticker - 1; if (currentposition == actualPosition) { continue; } int noOfPositionBribed = actualPosition - currentposition; if (noOfPositionBribed > 2) { isChaotic = true; break; } else if (noOfPositionBribed > 0 && noOfPositionBribed <= 2) { totalpositionMoved += noOfPositionBribed; for(int j=currentposition;j<actualPosition;j++) { int temp = arr[j]; int temp2 = arr[j+1]; int tempkeyval = map[temp]; arr[j] = temp2; map[temp] = map[temp2]; arr[j+1] = temp; map[temp2] = tempkeyval; } continue; } } if (isChaotic) { Console.WriteLine("Too chaotic"); } else { Console.WriteLine(totalpositionMoved); } }
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
This works (y).
public static void minimumBribes(List q) { var arr = q.ToArray(); int totalpositionMoved = 0; bool isChaotic = false; //