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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code