Python 3238.py
class Solution:
def winningPlayerCount(self, n: int, pick: List[List[int]]) -> int:
result = 0
all = defaultdict(dict)
for player, color in pick:
all[player][color] = all[player].get(color, 0) + 1
for player in all:
if max(all[player].values()) > player:
result += 1
return result
Difficulty
and has an acceptance rate of about 61% (80k+ submissions).
Typical patterns to solve this problem:
- Array
- Counting
- Hash Table
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a method that counts how many distinct players satisfy a specific winning condition. Each entry in the input pick represents a player identifier together with a color identifier. The method aggregates the number of times each player selects each color, then checks whether the highest count for any color exceeds the player’s own identifier. The final integer result reflects the number of players that meet this criterion.
Key Steps
Aggregating picks per player
all = defaultdict(dict)
for player, color in pick:
all[player][color] = all[player].get(color, 0) + 1
In this phase the code creates a nested dictionary where the outer key is the player ID and the inner dictionary maps each color to the number of times that player chose it. The use of defaultdict(dict) guarantees that a new inner dictionary is created automatically for each previously unseen player, eliminating the need for explicit existence checks. The get method supplies a default count of zero, allowing the increment operation to be performed in a single expression.
Evaluating the winning condition
for player in all:
if max(all[player].values()) > player:
result += 1
After the aggregation, the method iterates over each player’s color-frequency map. It extracts the maximum frequency among all colors chosen by that player and compares it to the player’s identifier. If the maximum frequency is strictly greater than the player ID, the player is considered a winner and the running total result is incremented.
Returning the final count
return result
The method concludes by returning the accumulated count of winning players.
Edge Cases Handled
When the input list pick is empty, the aggregation loop never executes, leaving the all dictionary empty. Consequently, the subsequent iteration over all also skips, and the method returns the initial value of result, which is zero. This correctly reflects that no players can win without any picks.
If a player appears multiple times with the same color, the aggregation logic correctly increments the count for that color, ensuring that the frequency reflects the total number of selections rather than a simple presence flag. Conversely, if a player selects many different colors, each color receives its own entry in the inner dictionary, and the max function will correctly identify the most frequently chosen color for comparison.
The comparison max(all[player].values()) > player gracefully handles cases where a player identifier is zero or negative. Since the frequency counts are non-negative integers, the condition will only be true when the player has selected a color more times than the numeric value of their identifier, which aligns with the intended rule regardless of the sign of the identifier.
Performance Optimizations
The algorithm leverages a hash-based dictionary structure for both the outer and inner mappings, providing constant-time average complexity for insertion and lookup operations. By updating the count in place during the first pass, the code avoids a secondary traversal that would be required if a separate counting phase were introduced. The use of defaultdict eliminates conditional branching for dictionary initialization, reducing overhead in tight loops.
During the evaluation phase, the max function operates on the collection of frequency values for a single player. Because each player’s inner dictionary contains only the colors they have actually chosen, the size of this collection is bounded by the distinct colors selected by that player, not by the total number of picks. This localized computation keeps the per-player work proportional to the player’s own activity, contributing to overall efficiency when the distribution of picks is skewed across many players.
ELI5 - Simplified Explanation
Imagine there is a big game with different players. Each player is given a number to identify them. The players take turns picking up colorful balls. Some players might pick up a lot of red ones, while others pick up blue or green ones.
The code starts by creating a special list to keep track of how many balls of each color every player has collected. It goes through the pile of all the balls that were picked and organizes them by putting each ball into the right player's bag.
Once that is done, the code looks at each player's bag to see what they have gathered. It checks if any player managed to collect more balls of a single color than their own special number. If a player has enough of one color, they get counted as a winner. The final result tells how many players succeeded in this challenge.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||
|---|---|---|---|---|---|---|
| author | solutions | |||||
| www | AlgoMonster | Java | C++ | Python | TS | |
| WalkCCC | Java | C++ | Python | |||
| leetcode.ca | Java | C++ | Go | Python | TS | |
| Progiez | Java | C++ | Python | |||
| github | charles-wangkai | Java | ||||
| fsq | C++ | |||||
| varunu28 | Java | |||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||
| WalkCCC | C++, Python, Java | |||||
| leetcode.ca | C++, Go, Java, Python, TypeScript | |||||
| Progiez | C++, Java, Python | |||||
| charles-wangkai | Java | |||||
| fsq | C++ | |||||
| varunu28 | Java |
Videos
Similar Problems
- problem 464: Can I Win
- problem 486: Predict the Winner
Solved On The Same Day
- problem 3222: Find the Winning Player in Coin Game
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality