[LeetCode] Find the Number of Winning Players

3238. Find the Number of Winning Players

You are given an integer n representing the number of players in a game and a 2D array pick where pick[i] = [xi, yi] represents that the player xi picked a ball of color yi.

Player i wins the game if they pick strictly more than i balls of the same color. In other words,

  • Player 0 wins if they pick any ball.
  • Player 1 wins if they pick at least two balls of the same color.
  • Player i wins if they pick at leasti + 1 balls of the same color.

Return the number of players who win the game.

Note that multiple players can win the game.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int winningPlayerCount(int n, vector<vector<int>>& pick) {
unordered_map<int, unordered_map<int,int>> freq;
unordered_set<int> us;
for(auto& vec : pick) {
int x = vec[0], y = vec[1];
if(++freq[x][y] > x) us.insert(x);
}
return us.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/08/04/PS/LeetCode/find-the-number-of-winning-players/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.