Skip to content

3238. Find the Number of Winning Players 👍

  • Time: $O(n + |\texttt{pick}|)$
  • Space: $O(10n) = O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
  public int winningPlayerCount(int n, int[][] pick) {
    final int MAX_COLOR = 10;
    int ans = 0;
    int[][] counts = new int[n][MAX_COLOR + 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;
  }
}
1
2
3
4
5
6
7
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))