We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defminimumBribes(q):bribe_times=0smallest_n_from_left=q[-1]# Traverse the queue from the second-to-last person to the firstforiinrange(len(q)-2,-1,-1):offset=q[i]-(i+1)# Check if the current person has moved more than two positions forwardifoffset>2:print('Toochaotic')return# Update bribe_times based on the offsetifoffset>0:bribe_times+=offsetelse:# Count bribe if current person is greater than smallest seen from the leftifq[i]>smallest_n_from_left:bribe_times+=1# Update the smallest number seen from the leftelse:smallest_n_from_left=q[i]print(bribe_times)
Cookie support is required to access HackerRank
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 →