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.
Python - the key for me was to think about 'how many people to the right have numbers smaller than me' as those people must have been overtaken (bribed). Then it became a matter of doing the check in a fast enough way to handle the test cases...
defminimumBribes(q):deffind_smaller_than(n,sorted_list):# fast (enough) as we stop looking once n > n_biggersmaller_than=0fornumberinsorted_list:ifnumber<n:smaller_than+=1else:returnsmaller_thanreturnsmaller_thanbribes=0remaining_queue=sorted(q)forpersoninq:remaining_queue.remove(person)overtaken=find_smaller_than(person,remaining_queue)ifovertaken>2:print("Too chaotic")returnbribes+=overtakenprint(bribes)
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 →
Python - the key for me was to think about 'how many people to the right have numbers smaller than me' as those people must have been overtaken (bribed). Then it became a matter of doing the check in a fast enough way to handle the test cases...