Problem 281074 · medium · Level 02 Linear Data Structures

Rush Jobs at the Copy Shop

queues · deque · simulation

A copy shop prints from a single queue of jobs, and priorities[i] (1 to 9) is how urgent the job in place i is. Again and again the operator picks up the job at the front:

  • if some other job still waiting in the queue is strictly more urgent, this one goes back to the end of the queue;
  • otherwise it is printed and leaves the queue.

The customer whose job started in place k wants to know when it comes out: return its place in the printing order, counting from 1.

Examples

Input:  priorities = [1, 3, 2, 3], k = 3
Output: 2
Explanation: the 1 moves to the back, the first 3 prints, the 2 moves to the back (a 3 is still waiting), then job 3 prints second.

Input:  priorities = [2, 6, 2, 3, 6], k = 0
Output: 4
Explanation: both 6s print first, then the 3; job 0 has gone to the back three times and prints fourth.

Constraints

  • 1 <= len(priorities) <= 500
  • 1 <= priorities[i] <= 9
  • 0 <= k < len(priorities)
  • Target: O(n**2) simulation is acceptable at this size; avoid rescanning the queue for its maximum on every step

Goals

  • Rotate a queue while carrying each job's original index
  • Check 'is there a higher priority behind me' without scanning the whole queue
Starting Python…