You are viewing a single comment's thread. Return to all comments →
Iterate from right to left, if you have seen 3 values lower than the current it's too chaotic, othewise add the seen list index to the bribe count.
bisect used to maintain order, only need to keep the 3 smallest values seen.
from bisect import bisect_left def minimumBribes(q): bribes = 0 seen = [] for i in range(len(q)-1, -1, -1): v = q[i] j = bisect_left(seen, v) if j >= 3: print('Too chaotic') return bribes += j seen.insert(j, v) seen = seen[:3] print(bribes)
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 →
Iterate from right to left, if you have seen 3 values lower than the current it's too chaotic, othewise add the seen list index to the bribe count.
bisect used to maintain order, only need to keep the 3 smallest values seen.