Open leeway00 opened 2 years ago
Fastest
def minimumBribes(q):
# Write your code here
bribes = 0
mins = (sys.maxsize, sys.maxsize)
for i, p in reversed([*enumerate(q, 1)]):
if p-i >2:
print("Too chaotic")
return
elif p > mins[1]:
bribes += 2
elif p > mins[0]:
bribes +=1
if p < mins[0]: # smallest after i-th
mins = (p, mins[0])
elif p < mins[1]: # second smallest after i-th
mins = (mins[0], p)
print(bribes)
def minimumBribes(Q):
moves = 0
# Loop through each person (P) in the queue (Q)
for i,P in enumerate(Q,1):
if P - i > 2:
print("Too chaotic")
return
for j in range(max(P-2,0),i): # it can be i-1
# Based on the P's current location i-1, (just use i because no duplicate)
# find out how many Q[j] in location j s.t. P-2<j<i-1 bribed P
if Q[j] > P:
moves += 1
print(moves)
Brute force