[LeetCode] Find The First Player to win K Games in a Row

3175. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int findWinningPlayer(vector<int>& A, int k) {
int ma = 0;
for(int i = 0; i < A.size(); i++) {
if(A[ma] < A[i]) ma = i;
int j = i + 1;
while(j < A.size() and A[i] > A[j]) j++;
int x = j - i - (i == 0);
if(x >= k) return i;
i = j - 1;
}
return ma;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/06/08/PS/LeetCode/find-the-first-player-to-win-k-games-in-a-row/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.