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
Winning Players: Count Color Balls and Check Conditionspicks = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]Player 0Picks: 1 ball color 0Needs: ≥1 same colorWINS! ✓Player 1Picks: 2 balls color 0Needs: ≥2 same colorWINS! ✓Player 2Picks: 2×color1, 1×color0Needs: ≥3 same colorLOSES ✗Player 3Picks: 0 ballsNeeds: ≥4 same colorLOSES ✗Rule: Player i wins if they have > i balls of the same colorAnswer: 2 winning players
Understanding the Visualization
1
Input
n=4 players, picks=[[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]
2
Process
Count each player's balls per color and check winning conditions
3
Output
2 players win the game
Key Takeaway
🎯 Key Insight: Player i needs at least i+1 balls of the same color to win
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