A competition consists of n
players numbered from 0
to n - 1
.
You are given an integer array skills
of size n
and a positive integer k
, where skills[i]
is the skill level of player i
. All integers in skills
are unique.
All players are standing in a queue in order from player 0
to player n - 1
.
The competition process is as follows:
The winner of the competition is the first player who wins k
games in a row.
Return the initial index of the winning player.
Example 1:
Input: skills = [4,2,6,3,9], k = 2
Output: 2
Explanation:
Initially, the queue of players is [0,1,2,3,4]
. The following process happens:
[0,2,3,4,1]
.[2,3,4,1,0]
.[2,4,1,0,3]
.Player 2 won k = 2
games in a row, so the winner is player 2.
Example 2:
Input: skills = [2,5,4], k = 3
Output: 1
Explanation:
Initially, the queue of players is [0,1,2]
. The following process happens:
[1,2,0]
.[1,0,2]
.[1,2,0]
.Player 1 won k = 3
games in a row, so the winner is player 1.
Constraints:
n == skills.length
2 <= n <= 105
1 <= k <= 109
1 <= skills[i] <= 106
skills
are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach is all about trying every single possibility to see which one works. In this game scenario, it means checking every possible sequence of games to see if either player wins K games in a row. We examine each potential starting point and consecutive sequence length.
Here's how the algorithm would work step-by-step:
def find_first_winner(game_results, consecutive_wins_needed):
number_of_games = len(game_results)
for starting_game_index in range(number_of_games):
winning_player = game_results[starting_game_index]
current_streak_length = 1
# Check for consecutive wins starting from the current game
for next_game_index in range(starting_game_index + 1, number_of_games):
if game_results[next_game_index] == winning_player:
current_streak_length += 1
else:
break
# If the streak reaches the required length, we found the winner
if current_streak_length == consecutive_wins_needed:
return winning_player
# If no player reached the required consecutive wins
return None
The problem involves determining which player wins a series of games by being the first to achieve a specific number of consecutive wins. The optimal approach focuses on tracking consecutive wins efficiently and checking if either player reaches the winning condition in each step.
Here's how the algorithm would work step-by-step:
def find_first_winner(results, consecutive_wins):
player1_wins = 0
player2_wins = 0
for result in results:
if result == 1:
# Player 1 won, so increment their wins.
player1_wins += 1
player2_wins = 0
else:
# Player 2 won, so increment their wins.
player2_wins += 1
player1_wins = 0
# Check if player 1 has reached the target wins.
if player1_wins == consecutive_wins:
return 1
# Check if player 2 has reached the target wins.
if player2_wins == consecutive_wins:
return 2
# No player reached the required number of wins in a row.
return 0
Case | How to Handle |
---|---|
wins array is null or empty | Return -1 immediately, as no player can win if there are no games. |
k is less than or equal to 0 | Return -1 immediately, as it's impossible to win a non-positive number of games in a row. |
k is greater than n (length of wins array) | Return -1 immediately, as it's impossible to win more games in a row than total games played. |
wins array contains values other than 1 or 2 | Treat any value other than 1 or 2 as an invalid game and potentially return -1 if the valid game values do not lead to a winner. |
wins array contains all 1s or all 2s and k is less than or equal to n | The first player (1 or 2) will win with k games in a row. |
No player wins k games in a row | Iterate through the entire wins array and return -1 if no player achieves k consecutive wins. |
n is very large (e.g., 10^5) and k is small (e.g., 2) | The sliding window approach avoids recomputing counts for each position, ensuring efficiency even with large n. |
n is very large and k is also very large (close to n) | The sliding window approach still works; however, if performance is critical, consider optimizations if there is information about the statistical distribution of wins (e.g., mostly 1s or mostly 2s). |