Priority Printer
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
A printer prints more important documents first. Its queue holds the documents in the order of priorities, where
priorities[i] (1 to 9, higher is more important) is the importance of document i. The printer repeats:
- Take the document at the front of the queue.
- If any document still in the queue has a higher priority, put the taken document at the back of the queue.
- Otherwise print it.
Return the position (1-based) at which the document initially at index location is printed.
Examples
| priorities | location | result |
|---|---|---|
[2, 1, 3, 2] | 2 | 1 |
[1, 1, 9, 1, 1, 1] | 0 | 5 |
[1, 2, 3, 2, 1] | 4 | 4 |
Example 3 — the print order is documents 2, 3, 1, 4, 0: after document 3 is printed the queue is 4, 0, 1, so documents 4 and 0 are moved to the back before document 1 is printed.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(priorities: int[], location: int) → int
Constraints
-
priorities.length -
priorities[i] -
locationpriorities.length
Expected complexity
- Time
- O(N)
- Space
- O(N)
Examples
| priorities | location | Expected |
|---|---|---|
[2,1,3,2] | 2 | 1 |
[1,1,9,1,1,1] | 0 | 5 |
[1,2,3,2,1] | 4 | 4 |
Hint
Hint
Moving a document to the back never changes the cyclic order of the remaining documents, so the queue is always the remaining documents in original order, rotated. Documents of priority 9 are printed in cyclic order from the current start, then the start moves to just after the last one printed, and so on down to priority 1: in total. Simulating the queue and scanning it for a more important document at every step is and exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 3 | verdict only |