1139. Priority Print Turn
A print server keeps jobs in a first-in first-out queue. Each job has a priority from 1 to 9 given by priorities, where 9 is the most urgent. Repeatedly look at the job at the front of the queue: if some job still waiting in the queue has a strictly higher priority, move the front job to the back; otherwise print it and remove it. Jobs with equal priority never block each other.
Return the 1-based turn on which the job at original index k is printed. Simulate the queue and keep a count of waiting jobs per priority to test for a higher one. Time is O(n^2) in the worst case, which is fine for n up to 100, and space is O(n).
Example 1
- Input:
- priorities = [1,3,2], k = 0
- Output:
- 3
- Explanation:
Job 0 (priority 1) is sent to the back once because higher priorities wait, so job 1 prints first, job 2 second and job 0 third.
Example 2
- Input:
- priorities = [4,4,4], k = 1
- Output:
- 2
- Explanation:
Equal priorities never block each other, so the jobs print in queue order and job 1 prints on turn 2.
Constraints
1 ≤ priorities.length ≤ 100
1 ≤ priorities[i] ≤ 9
0 ≤ k < priorities.length
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n^2)
- Space
- O(n)