You are viewing a single comment's thread. Return to all comments →
C# O(n)
public static void minimumBribes(List<int> q) { const int maxBribes = 2; var totalBribes = 0; var head = new SortedLinkedList(0); for (var i = 1; i <= q.Count; i++) { if (!head.TryInsert(q[^i], maxBribes, out var shifts)) { Console.WriteLine("Too chaotic"); return; } totalBribes += shifts; } Console.WriteLine(totalBribes); } internal record SortedLinkedList(int N) { private SortedLinkedList? _next; public bool TryInsert(int number, int maxShifts, out int shifts) { shifts = 0; var current = this; for (var i = 0; i < maxShifts; i++) { if (current._next == null || current._next.N > number) break; shifts++; current = current._next; } if (current._next != null && current._next.N < number) return false; var node = new SortedLinkedList(number); node._next = current._next; current._next = node; return true; } }
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# O(n)