1701. Champion Rotation Queries
A ladder tournament keeps its fighters in a line, where line[i] is the strength of the fighter standing at position i. In every round the two fighters at the front, A (first) and then B (second), step out and duel: the stronger one returns to the front of the line and the weaker one goes to the very back. If the strengths are equal, A counts as the winner and returns to the front. Rounds are numbered from 1 and never stop.
Given line and an array queries of round numbers, return an int[][] with one pair [A, B] per query, in the same order: the strengths of the two fighters that stepped out in that round, A first. Queries may repeat. Round numbers go up to 10^15, so rounds cannot be played one by one. Play only the first n - 1 rounds; after that the strongest fighter stays at the front and the others cycle with period n - 1. Target O(n + q) time and O(n) extra space.
Example 1
- Input:
- line = [3,1,4,2], queries = [1,2,3,4,5,6]
- Output:
- [[3,1],[3,4],[4,2],[4,1],[4,3],[4,2]]
- Explanation:
Round 1 is (3, 1) and 3 stays in front; round 2 is (3, 4) and 4 takes the front; round 3 is (4, 2); from round 4 the champion 4 meets 1, 3, 2 in turn.
Example 2
- Input:
- line = [5,5,5], queries = [1,2,3,1000000000000000]
- Output:
- [[5,5],[5,5],[5,5],[5,5]]
- Explanation:
All strengths are equal, so every round is (5, 5) with A keeping the front, even for round 10^15.
Example 3
- Input:
- line = [1,2], queries = [1,2,3]
- Output:
- [[1,2],[2,1],[2,1]]
- Explanation:
With two fighters the round-1 duel is (1, 2); after that 2 stays in front and 1 comes back to the second place every round.
Constraints
2 ≤ n = line.length ≤ 105
0 ≤ line[i] ≤ 109
1 ≤ queries.length ≤ 105
1 ≤ queries[j] ≤ 1015
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n + q)
- Space
- O(n)