3175. Find The First Player to win K Games in a Row LeetCode Solution
In this guide, you will get 3175. Find The First Player to win K Games in a Row LeetCode Solution with the best time and space complexity. The solution to Find The First Player to win K Games in a Row problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find The First Player to win K Games in a Row solution in C++
- Find The First Player to win K Games in a Row solution in Java
- Find The First Player to win K Games in a Row solution in Python
- Additional Resources
Problem Statement of Find The First Player to win K Games in a Row
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 first two players in the queue play a game, and the player with the higher skill level wins.
After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.
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:
Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is [0,2,3,4,1].
Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is [2,3,4,1,0].
Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is [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:
Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].
Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is [1,0,2].
Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [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
All integers in skills are unique.
Complexity Analysis
- Time Complexity:
- Space Complexity:
3175. Find The First Player to win K Games in a Row LeetCode Solution in C++
class Solution {
public:
// Similar to 1535. Find the Winner of an Array Game
int findWinningPlayer(vector<int>& skills, int k) {
int ans = 0;
int wins = 0;
for (int i = 1; i < skills.size() && wins < k; ++i)
if (skills[i] > skills[ans]) {
ans = i;
wins = 1;
} else {
++wins;
}
return ans;
}
};
/* code provided by PROGIEZ */
3175. Find The First Player to win K Games in a Row LeetCode Solution in Java
class Solution {
// Similar to 1535. Find the Winner of an Array Game
public int findWinningPlayer(int[] skills, int k) {
int ans = 0;
int wins = 0;
for (int i = 1; i < skills.length && wins < k; ++i)
if (skills[i] > skills[ans]) {
ans = i;
wins = 1;
} else {
++wins;
}
return ans;
}
}
// code provided by PROGIEZ
3175. Find The First Player to win K Games in a Row LeetCode Solution in Python
class Solution:
# Similar to 1535. Find the Winner of an Array Game
def findWinningPlayer(self, skills: list[int], k: int) -> int:
ans = 0
wins = 0
i = 1
while i < len(skills) and wins < k:
if skills[i] > skills[ans]:
ans = i
wins = 1
else:
wins += 1
i += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.