3238. Find the Number of Winning Players LeetCode Solution

In this guide, you will get 3238. Find the Number of Winning Players LeetCode Solution with the best time and space complexity. The solution to Find the Number of Winning Players 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

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Number of Winning Players solution in C++
  4. Find the Number of Winning Players solution in Java
  5. Find the Number of Winning Players solution in Python
  6. Additional Resources
3238. Find the Number of Winning Players LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]
Output: 2
Explanation:
Player 0 and player 1 win the game, while players 2 and 3 do not win.

Example 2:

Input: n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]
Output: 0
Explanation:
No player wins the game.

Example 3:

See also  2402. Meeting Rooms III LeetCode Solution

Input: n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]
Output: 1
Explanation:
Player 2 wins the game by picking 3 balls with color 4.

Constraints:

2 <= n <= 10
1 <= pick.length <= 100
pick[i].length == 2
0 <= xi <= n – 1
0 <= yi <= 10

Complexity Analysis

  • Time Complexity: O(n + |\texttt{pick}|)
  • Space Complexity: O(10n) = O(n)

3238. Find the Number of Winning Players LeetCode Solution in C++

class Solution {
 public:
  int winningPlayerCount(int n, vector<vector<int>>& pick) {
    constexpr int kMaxColor = 10;
    int ans = 0;
    vector<vector<int>> counts(n, vector<int>(kMaxColor + 1));

    for (const vector<int>& p : pick) {
      const int player = p[0];
      const int color = p[1];
      ++counts[player][color];
    }

    for (int i = 0; i < n; ++i) {
      int maxCount = 0;
      for (const int freq : counts[i])
        maxCount = max(maxCount, freq);
      if (maxCount > i)
        ++ans;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3238. Find the Number of Winning Players LeetCode Solution in Java

class Solution {
  public int winningPlayerCount(int n, int[][] pick) {
    final int kMaxColor = 10;
    int ans = 0;
    int[][] counts = new int[n][kMaxColor + 1];

    for (int[] p : pick) {
      final int player = p[0];
      final int color = p[1];
      ++counts[player][color];
    }

    for (int i = 0; i < n; ++i) {
      int maxCount = 0;
      for (final int freq : counts[i])
        maxCount = Math.max(maxCount, freq);
      if (maxCount > i)
        ++ans;
    }

    return ans;
  }
}
// code provided by PROGIEZ

3238. Find the Number of Winning Players LeetCode Solution in Python

class Solution:
  def winningPlayerCount(self, n: int, pick: list[list[int]]) -> int:
    counts = [collections.Counter() for _ in range(n)]
    for player, color in pick:
      counts[player][color] += 1
    return sum(max(count.values(), default=0) > i
               for i, count in enumerate(counts))
# code by PROGIEZ

Additional Resources

See also  2303. Calculate Amount Paid in Taxes LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.