Find the Winner of an Array Game
Real-world version: Ladder Winner On The Practice LaneThe statement here is our own: the same inputs, outputs and technique.
A club runs a ladder on its practice lane. ratings lists the players waiting in one queue from front to back, so ratings[0] is at the very front, and every rating in the list is different.
A round is played like this: the two players at the front of the queue face each other, the one with the higher rating stays where it is at the front, and the other walks to the very back of the queue. Rounds are played one after another until some player has won k rounds in a row, counting only rounds played since that player last arrived at the front. That player takes the ladder.
Return the rating of the player who takes the ladder.
Examples
Example 1
- Input
- ratings = [1, 4, 2, 9], k = 2
- Output
- 4
Round one is 1 against 4, so 4 stays at the front and 1 goes to the back, leaving the queue 4, 2, 9, 1. Round two is 4 against 2, so 4 wins again. That is two rounds in a row for 4, which is what `k` asks for.
Example 2
- Input
- ratings = [3, 1, 2, 9, 8], k = 7
- Output
- 9
3 beats 1 and 2, then loses to 9 in the third round. From that round on 9 stands at the front and wins every round that follows, so its streak passes seven.
Example 3
- Input
- ratings = [2, 1], k = 1
- Output
- 2
The queue holds only two players. 2 beats 1 in the first round, and one win in a row is all `k` asks for.
Constraints
- 2 <= ratings.length <= 10^5
- 1 <= ratings[i] <= 10^6
- 1 <= k <= 10^9
- All ratings are different.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def ladder_winner(ratings: list[int], k: int) -> int:public int ladderWinner(int[] ratings, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Concatenation of ArrayReal-world version: Two Cycle PlayoutEasy
- Reshape the MatrixReal-world version: Relaying the Contact SheetEasy
- Separate the Digits in an ArrayReal-world version: Digit Cells for the Ticket CodesEasy
- Distribute Elements Into Two Arrays IReal-world version: Splitting Parcels Onto Two Intake TraysEasy