Problem 281074 · medium · Phase 02 Linear Data Structures

Priority Printer Queue

queues · deque · simulation

A shared printer holds a queue of jobs; priorities[i] is the priority (1 to 9) of the job that is i-th in line. The printer repeatedly looks at the job at the front:

  • if any job anywhere behind it has a strictly higher priority, the front job is moved to the back of the queue;
  • otherwise the front job is printed and removed.

Return the position (1-based) at which the job that started at index k is printed.

Examples

Input:  priorities = [2, 1, 3, 2], k = 2
Output: 1
Explanation: the priority-3 job is printed first.

Input:  priorities = [1, 1, 9, 1, 1, 1], k = 0
Output: 5
Explanation: the 9 goes first, then the jobs behind it in order (indices 3, 4, 5), then index 0.

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…