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.
This will help you understand and solve this better.
defminimumBribes(q):"""SlidingWindowApproach.Imagineaqueueofpeopleinlineforaride.whereeachpersonhaveanumbertag.Andyouareapolicemanstandingattheendoftheline.Assumethereare8peopleinline."""# As you dnt know about how many bribes have been done, bribes# count is 0.bribes=0# Moving towards the start of the line and checking for bribes. i = 0.foriinrange(len(q)):# j is the person you are standing next to. j = 8 - 1 - 0 = 7.j=len(q)-1-i# check if the person in front of j (j-1) has tag of j (j+1) then he bribed j.ifq[j-1]==j+1:# Swap them to fix the queue.q[j],q[j-1]=q[j-1],q[j]# Take the bribe money from j ;)bribes+=1# check if the person next to the person in front j (j-2)# if he has tag of j (j+1) then he bribed both.elifq[j-2]==j+1:# Swap them to fix the queue.q[j],q[j-1],q[j-2]=q[j-2],q[j],q[j-1]# Take the bribe money from both ;)bribes+=2# Now if not the both above cases, check the person next to you j.# If he got the correct tag do nothing. if not, arrest everyone as# you have proof that the bribes are more than 2. :oelifq[j]!=j+1:print('Toochaotic')return# If not Too Chaotic take the bribe money and go home. You Deserve it. :)print(bribes)
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
This will help you understand and solve this better.