Show problems as
Classic problem #1535MediumArraySimulation

Find the Winner of an Array Game

Real-world version: Ladder Winner On The Practice Lane
Tracked in this browser only
Read Find the Winner of an Array Game on LeetCode(opens in a new tab)

The 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.

Python
def ladder_winner(ratings: list[int], k: int) -> int:
Java
public int ladderWinner(int[] ratings, int k)