Find The First Player to win K Games in a Row - Problem

Imagine a competitive gaming tournament where players are lined up in a queue, each with a unique skill level. The tournament follows a simple but exciting format:

  • The first two players in the queue compete
  • The player with the higher skill level wins
  • The winner stays at the front of the queue
  • The loser goes to the back of the queue

The ultimate champion is determined by being the first player to win k consecutive games. Your task is to simulate this tournament and find which player (by their original index) will be crowned the champion.

Input: An array skills representing each player's skill level and an integer k representing the number of consecutive wins needed.

Output: The original index of the player who wins k games in a row first.

Note: All skill levels are unique, so there are no ties!

Input & Output

example_1.py โ€” Basic Tournament
$ Input: skills = [4,2,6,3,9], k = 2
โ€บ Output: 2
๐Ÿ’ก Note: Player 0 (skill 4) beats Player 1 (skill 2). Then Player 2 (skill 6) beats Player 0. Player 2 then beats Player 3 (skill 3), winning 2 games in a row. Player 2 is at index 2.
example_2.py โ€” High K Value
$ Input: skills = [2,5,4], k = 3
โ€บ Output: 1
๐Ÿ’ก Note: Since k=3 โ‰ฅ n-1=2, the player with maximum skill will win. Player 1 has skill 5, which is the highest, so they win.
example_3.py โ€” Single Win Required
$ Input: skills = [1,9,8,2,3], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: Player 1 (skill 9) beats Player 0 (skill 1) in the first game and immediately wins since k=1.

Constraints

  • n == skills.length
  • 2 โ‰ค n โ‰ค 105
  • 1 โ‰ค k โ‰ค 109
  • 1 โ‰ค skills[i] โ‰ค 106
  • All integers in skills are unique

Visualization

Tap to expand
Tournament Championship๐Ÿ‘‘CHAMPIONWin Streak: 2/3P1P3Next ChallengerDefeated
Understanding the Visualization
1
Setup
Players line up to challenge the current champion
2
Challenge
Each player challenges the current champion based on skill level
3
Victory
Winner becomes/remains champion, increment win streak
4
Champion
First player to win k consecutive games is declared winner
Key Takeaway
๐ŸŽฏ Key Insight: We don't need to simulate the entire queue - just track the current winner and count consecutive wins. The optimization k โ‰ฅ n-1 โ†’ strongest player wins makes large k values trivial.
Asked in
Amazon 45 Google 38 Meta 25 Microsoft 20
43.7K Views
Medium Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen