Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A new rollercoaster has opened nearby and everyone is keen to see it . Unfortunately, it also has a long queue. Some people are bribing
A new rollercoaster has opened nearby and everyone is keen to see it Unfortunately, it also has a long queue. Some people are bribing those in front to move more quickly through the queue. You want to identify how many bribes have taken place.
You wait until the ride is stopped for maintenance and record the position of all people in the line. You record the positions of everyone in the queue, giving everyone a number from to N where person is at the front of the queue, person N is at the back. Any person can swap positions with the person in front of them, and only the person in front of them, for a small bribe Someone could move all the way through the queue by bribing multiple times. During this period noone leaves or joins the queue. After the maintenance finishes you again record the positions of everyone, which may now be jumbled up if bribes have taken place.
You have to figure out what is the smallest number if bribes that could have happened to produce a given queue order.
For example:
If the initial state of the queue is and the new state is then person has paid two bribes and you should return
If the initial state of the queue is and the new state is then persons and have each bribed the person in front and you should return
If the initial state of the queue is and the new state is then person made bribes to get to the front, person made bribes to get in front of and person made bribes etc. and so you should return
The queue can be very long, and may have as many as people in it
WARNING
As with the Sums question, this question can time out the server if your code is not efficient. Here we have not provided a default timeout condition. You may need to implement one here to pass the shorter test cases before attempting the long ones!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started