You are viewing a single comment's thread. Return to all comments →
C# solution with recursive
public static void minimumBribes(List<int> q) { var bribes=0; for(int i=q.Count-1;i>=0;i--) { int counter=0; Swap(q,i, ref counter); if(counter>2) { Console.WriteLine("Too chaotic"); return; } bribes+=counter; } Console.WriteLine(bribes); } public static void Swap(List<int> a, int i,ref int counter) { if(i>=a.Count) return; if(i==0) return; if(a[i - 1] > a[i]) { var item = a[i]; a[i] = a[i-1]; a[i-1] = item; counter++; Swap(a,++i,ref counter); } }
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
C# solution with recursive