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.
# Approach# Start with a queue that has zero bribes - with everyone in order# Move towards the current state of the bribed queue as you count the number of bribes done by someone# If they exceed three, stop and print 'Too chaotic'# Otherwise continue with bribing until you reach the final state of the bribed queuenumBribes={}positions={}rideQueue=Nonedefbribe(briberPos):globalrideQueuebribeePos=briberPos-1# Set the position of the briber to the one infront of himbriber=rideQueue[briberPos]bribee=rideQueue[briberPos-1]positions[briber]=briberPos-1# Set the position of the bribed to the briber's positionpositions[bribee]=briberPos# Update number of bribesnumBribes[briber]=numBribes.get(briber,0)+1# Switch places, bribe the person infront of the briberrideQueue[briberPos],rideQueue[bribeePos]=bribee,briber#print(rideQueue)defisFinalPosBribed(finalPos,currPos):# currPos is the position currently held in the queue as the bribes take placereturncurrPos>finalPosdefhasBriberExceededMaxBribes(briberId):returnnumBribes.get(briberId,0)>2defminimumBribes(q):globalrideQueueglobalnumBribesglobalpositionsnumBribes={}positions={}rideQueue=sorted(q)i=0whilei<len(q):finalPos=ibriberId=q[i]unbribedPos=q[i]-1# Get the current position of the person. Remember as the bribes take place, positions change, person 6 may endup infront of 4# Implying a person 4 can bribe 6 for as long as they are infront of themcurrPos=positions.get(q[i])orunbribedPosifisFinalPosBribed(finalPos,currPos):#print(f"Person: {briberId} bribed")whilecurrPos>finalPos:# Progress the person towards their bribed positionbribe(currPos)currPos-=1ifhasBriberExceededMaxBribes(briberId):print("Too chaotic")returnelse:...#print(f"Person: {briberId} didn't bribe")i+=1#print(rideQueue)print(sum(numBribes.values()))
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 Solution