Find the Number of Winning Players - Problem

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 (at least 1 ball of any color)
  • Player 1 wins if they pick at least 2 balls of the same color
  • Player i wins if they pick at least i + 1 balls of the same color

Return the number of players who win the game. Note that multiple players can win the game.

Input & Output

Example 1 — Basic Game
$ Input: n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]
Output: 2
💡 Note: Player 0 picks 1 ball of color 0, needs ≥1, wins. Player 1 picks 2 balls of color 0, needs ≥2, wins. Player 2 picks 2 balls of color 1 and 1 of color 0, needs ≥3 of same color, loses. Player 3 picks nothing, loses.
Example 2 — Single Winner
$ Input: n = 5, pick = [[1,3],[4,4],[1,3],[3,0]]
Output: 1
💡 Note: Player 1 picks 2 balls of color 3, needs ≥2, wins. Player 3 picks 1 ball of color 0, needs ≥4, loses. Player 4 picks 1 ball of color 4, needs ≥5, loses. Players 0 and 2 pick nothing, lose.
Example 3 — No Winners
$ Input: n = 3, pick = [[1,1],[2,2]]
Output: 0
💡 Note: Player 0 picks nothing, loses. Player 1 picks 1 ball of color 1, needs ≥2, loses. Player 2 picks 1 ball of color 2, needs ≥3, loses.

Constraints

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

Visualization

Tap to expand
Find the Number of Winning Players INPUT n = 4 (4 players) pick array: [0,0] [1,0] [1,0] [2,1] [2,1] [2,0] Players: P0 P1 P2 P3 P0 needs: 1+ ball same color P1 needs: 2+ balls same color P2 needs: 3+ balls same color P3 needs: 4+ balls same color ALGORITHM STEPS 1 Count balls per player Group by color for each 2 Build frequency map player --> {color: count} P0: {0:1} max=1 P1: {0:2} max=2 P2: {1:2, 0:1} max=2 P3: {} max=0 (color 0=blue, 1=red) 3 Check win condition max_count > i for player i 4 Count winners P0: 1>0 OK | P1: 2>1 OK P2: 2>2 NO | P3: 0>3 NO FINAL RESULT Winner Analysis: P0 1 ball color 0 1 > 0 = WINS! P1 2 balls color 0 2 > 1 = WINS! P2 2 balls color 1 2 > 2 = LOSES P3 0 balls picked 0 > 3 = LOSES Output: 2 Key Insight: Use a hash map to track the count of each color for each player. For player i, find the maximum count of any single color they picked. If max_count > i, player i wins. Time: O(n + picks), Space: O(n * colors). The key is tracking per-player color frequencies efficiently. TutorialsPoint - Find the Number of Winning Players | Optimal Solution
Asked in
Google 15 Microsoft 12 Amazon 8
23.4K Views
Medium Frequency
~12 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen