Ctrl K

King of the Table

Medium
A table tennis club plays winner-stays-on. Players are numbered from 0 to n - 1, player i has rating ratings[i], and all ratings are distinct. At the start, the players line up in order of their numbers. The first two players in line play a game, and the one with the higher rating wins. The winner stays at the front of the line, and the loser goes to the back. Then it repeats. The champion is the first player to win streak games in a row. Return that player's number.
Example 1
ratings=[3, 8, 5, 1, 9], streak=3 → 1

Player 1 (rating 8) beats player 0, then players 2 and 3: three wins in a row. Player 4 never gets to the table.

Example 2
ratings=[7, 2, 4], streak=1 → 0

One win is enough, and player 0 takes the very first game.

Example 3
ratings=[4, 6, 2], streak=1000 → 1

The streak is longer than the number of opponents, so in the end the strongest player, player 1, holds the table for good.

Constraints
  • 2 ≤ len(ratings) ≤ 10⁵
  • 1 ≤ streak ≤ 10⁹
  • 1 ≤ ratings[i] ≤ 10⁶
  • All values in ratings are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade Medium
loading Python…
Input
ratings=[3, 8, 5, 1, 9], streak=3
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 125 tests on the server, hidden ones included.