Talentd
FleetCode
Make and follow sheets.Sign in

Find the Number of Winning Players solution and explanation

EasyArrayCountingHash Table6 min read60.5% acceptanceUpdated 9 Apr 2026
Find the Number of Winning Players || Biweekly Contest 136 || Leetcode Solutioncodi · 0:51 · 1K viewsWatch on YouTube
7 more videos on this problem
  1. Find the Number of Winning Players || Biweekly Contest 136 || Leetcode Solutioncodi · 0:51 · 1K views
  2. Find the Number of Winning Players Solution | LEETCODE BIWEEKLY CONTEST 136 SOLUTIONJUST CODE · 0:49 · 639 views
  3. Leetcode | 3238. Find the Number of Winning Players | Easy | Java SolutionDeveloper Docs · 10:12 · 545 views
  4. 3238. Find the Number of Winning PlayersTech Courses · 7:05 · 488 views
  5. LeetCode Biweekly Contest 136 | Video Solutions | Find the Number of Winning PlayersSmart Interviews · 8:55 · 477 views
  6. leetcode 3238 : number of winning players : python solutionleetcode blind 75 · 4:56 · 354 views
  7. 3238. Find the Number of Winning Players (Leetcode Easy)Programming Live with Larry · 4:20 · 322 views
  8. Leetcode BiWeekly Contest 136 || Problems A, B, C, || 3238, 3239, 3240 || CodeaticCode-a-tic · 47:12 · 305 views

The 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.
  • 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:

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

Approach overview

Problem Overview: You are given n players and a list of ball picks where picks[i] = [player, color]. A player i wins if they collect strictly more than i balls of the same color. The task is to count how many players satisfy this winning condition.

Approach 1: Hash Map Approach (O(m) time, O(m) space)

Track how many balls of each color every player collects. Use a hash map keyed by (player, color) and increment the count while iterating through the picks array. Whenever the count for a pair exceeds the player's index player, mark that player as a winner. A set helps ensure each player is counted only once even if multiple colors exceed the threshold.

This approach works well when the number of colors is large or unknown. Hash lookups keep updates constant time, making the entire scan of picks linear. The technique relies on efficient frequency tracking using a hash table and simple counting.

Approach 2: Array Counting Approach (O(m) time, O(n * c) space)

If the number of colors is small or bounded, replace the hash map with a 2D counting array where count[player][color] stores how many balls of that color the player has picked. Iterate through picks, increment the corresponding cell, and check whether the count becomes greater than the player's index. Maintain a boolean array to mark winners so each player contributes to the result once.

This approach avoids hashing overhead and uses direct indexing. It performs well when color IDs are within a predictable range, making array access faster than map operations. The method is essentially a structured version of the same array frequency counting pattern.

Recommended for interviews: The hash map approach is typically expected because it works for any color range without assumptions. The array counting version is a good optimization when constraints guarantee small color values. Showing both demonstrates you understand frequency counting tradeoffs and space optimization.

Approach 1: Hash Map Approach

In this approach, we make use of a two-dimensional hash map. For each pick (where a player picks a ball of a certain color), we increment the count in our hash map. Once all picks are processed, we check each player to determine if they have picked more than the required number of balls of the same color to win.

def find_winning_players(n, pick):
    color_count = [{} for _ in range(n)]
    for player, color in pick:
        if color in color_count[player]:
            color_count[player][color] += 1
        else:
            color_count[player][color] = 1
    winning_players = 0
    for player in range(n):
        if any(count > player for count in color_count[player].values()):
            winning_players += 1
    return winning_players

# Example usage:
print(find_winning_players(4, [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]))  # Output: 2
import java.util.*;

public class Game {
    public static int findWinningPlayers(int n, int[][] pick) {
        Map<Integer, Map<Integer, Integer>> playerMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            playerMap.put(i, new HashMap<Integer, Integer>());
        }

        for (int[] p : pick) {
            int player = p[0];
            int color = p[1];
            Map<Integer, Integer> colorMap = playerMap.get(player);
            colorMap.put(color, colorMap.getOrDefault(color, 0) + 1);
        }

        int winningPlayers = 0;
        for (int i = 0; i < n; i++) {
            for (int count : playerMap.get(i).values()) {
                if (count > i) {
                    winningPlayers++;
                    break;
                }
            }
        }
        return winningPlayers;
    }

    public static void main(String[] args) {
        int[][] pick = {{0, 0}, {1, 0}, {1, 0}, {2, 1}, {2, 1}, {2, 0}};
        System.out.println(findWinningPlayers(4, pick)); // Output: 2
    }
}
#include <iostream>
#include <vector>
#include <unordered_map>

int findWinningPlayers(int n, std::vector<std::vector<int>>& pick) {
    std::vector<std::unordered_map<int, int>> color_count(n);
    for (auto& p : pick) {
        int player = p[0];
        int color = p[1];
        color_count[player][color]++;
    }

    int winning_players = 0;
    for (int i = 0; i < n; ++i) {
        for (auto& entry : color_count[i]) {
            if (entry.second > i) {
                ++winning_players;
                break;
            }
        }
    }
    return winning_players;
}

int main() {
    std::vector<std::vector<int>> pick = {{0, 0}, {1, 0}, {1, 0}, {2, 1}, {2, 1}, {2, 0}};
    std::cout << findWinningPlayers(4, pick) << std::endl; // Output: 2
    return 0;
}
function findWinningPlayers(n, pick) {
    const colorCount = Array.from({ length: n }, () => new Map());
    pick.forEach(([player, color]) => {
        if (colorCount[player].has(color)) {
            colorCount[player].set(color, colorCount[player].get(color) + 1);
        } else {
            colorCount[player].set(color, 1);
        }
    });

    let winningPlayers = 0;
    colorCount.forEach((map, player) => {
        for (let count of map.values()) {
            if (count > player) {
                winningPlayers++;
                break;
            }
        }
    });
    return winningPlayers;
}

// Example usage:
console.log(findWinningPlayers(4, [[0, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 0]])); // Output: 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_COLOR 11
#define MAX_PLAYERS 10

int color_count[MAX_PLAYERS][MAX_COLOR];

int findWinningPlayers(int n, int pick[][2], int pickSize) {
    memset(color_count, 0, sizeof(color_count));
    for (int i = 0; i < pickSize; i++) {
        int player = pick[i][0];
        int color = pick[i][1];
        color_count[player][color]++;
    }

    int winningPlayers = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < MAX_COLOR; j++) {
            if (color_count[i][j] > i) {
                winningPlayers++;
                break;
            }
        }
    }
    return winningPlayers;
}

int main() {
    int pick[6][2] = {{0, 0}, {1, 0}, {1, 0}, {2, 1}, {2, 1}, {2, 0}};
    printf("%d\n", findWinningPlayers(4, pick, 6));
    return 0;
}
using System;
using System.Collections.Generic;

class Game {
    public static int FindWinningPlayers(int n, int[][] pick) {
        var colorCount = new Dictionary<int, Dictionary<int, int>>();
        for (int i = 0; i < n; i++) {
            colorCount[i] = new Dictionary<int, int>();
        }

        foreach (var p in pick) {
            int player = p[0];
            int color = p[1];
            if (!colorCount[player].ContainsKey(color)) {
                colorCount[player][color] = 0;
            }
            colorCount[player][color]++;
        }

        int winningPlayers = 0;
        for (int i = 0; i < n; i++) {
            foreach (var count in colorCount[i].Values) {
                if (count > i) {
                    winningPlayers++;
                    break;
                }
            }
        }
        return winningPlayers;
    }

    static void Main() {
        int[][] pick = { new int[] {0, 0}, new int[] {1, 0}, new int[] {1, 0}, new int[] {2, 1}, new int[] {2, 1}, new int[] {2, 0} };
        Console.WriteLine(FindWinningPlayers(4, pick)); // Output: 2
    }
}

This solution uses a list of dictionaries, where each dictionary corresponds to a player. The keys of the dictionary are the colors, and the values are the counts of that color picked by the player. We iterate through each player's dictionary and check if any color has been picked more times than required. If so, we increment the count of winning players.

Complexity

Time Complexity: O(p) where p is the number of entries in the 'pick' list.

Space Complexity: O(n * c) where n is the number of players and c is the number of colors.

Try this approach in the editor

Approach 2: Array Counting Approach

This method involves using a simple array structure to count the number of balls each player has picked by color, taking advantage of maximum constraints. This reduces overhead but is less flexible than hash maps.

def find_winning_players_v2(n, pick):
    color_count = [[0] * 11 for _ in range(n)]
    for player, color in pick:
        color_count[player][color] += 1

    winning_players = 0
    for player in range(n):
        if any(count > player for count in color_count[player]):
            winning_players += 1
    return winning_players

# Example usage:
print(find_winning_players_v2(4, [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]))  # Output: 2
public class GameV2 {
    public static int findWinningPlayersV2(int n, int[][] pick) {
        int[][] colorCount = new int[n][11];

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

        int winningPlayers = 0;
        for (int i = 0; i < n; i++) {
            for (int count : colorCount[i]) {
                if (count > i) {
                    winningPlayers++;
                    break;
                }
            }
        }
        return winningPlayers;
    }

    public static void main(String[] args) {
        int[][] pick = { {0, 0}, {1, 0}, {1, 0}, {2, 1}, {2, 1}, {2, 0} };
        System.out.println(findWinningPlayersV2(4, pick)); // Output: 2
    }
}
#include <iostream>
#include <vector>

int findWinningPlayersV2(int n, std::vector<std::vector<int>>& pick) {
    int color_count[10][11] = {0};
    for (auto& p : pick) {
        int player = p[0];
        int color = p[1];
        color_count[player][color]++;
    }

    int winning_players = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 11; ++j) {
            if (color_count[i][j] > i) {
                ++winning_players;
                break;
            }
        }
    }
    return winning_players;
}

int main() {
    std::vector<std::vector<int>> pick = {{0, 0}, {1, 0}, {1, 0}, {2, 1}, {2, 1}, {2, 0}};
    std::cout << findWinningPlayersV2(4, pick) << std::endl; // Output: 2
    return 0;
}
function findWinningPlayersV2(n, pick) {
    const colorCount = Array.from({ length: n }, () => Array(11).fill(0));
    pick.forEach(([player, color]) => {
        colorCount[player][color]++;
    });

    let winningPlayers = 0;
    colorCount.forEach((counts, player) => {
        if (counts.some(count => count > player)) {
            winningPlayers++;
        }
    });
    return winningPlayers;
}

// Example usage:
console.log(findWinningPlayersV2(4, [[0, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 0]])); // Output: 2
#include <stdio.h>
#include <string.h>

int findWinningPlayersV2(int n, int pick[][2], int pickSize) {
    int color_count[10][11] = {0};
    for (int i = 0; i < pickSize; i++) {
        int player = pick[i][0];
        int color = pick[i][1];
        color_count[player][color]++;
    }

    int winningPlayers = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 11; j++) {
            if (color_count[i][j] > i) {
                winningPlayers++;
                break;
            }
        }
    }
    return winningPlayers;
}

int main() {
    int pick[6][2] = {{0, 0}, {1, 0}, {1, 0}, {2, 1}, {2, 1}, {2, 0}};
    printf("%d\n", findWinningPlayersV2(4, pick, 6));
    return 0;
}
using System;

class GameV2 {
    public static int FindWinningPlayersV2(int n, int[][] pick) {
        int[,] colorCount = new int[n, 11];

        foreach (var p in pick) {
            int player = p[0];
            int color = p[1];
            colorCount[player, color]++;
        }

        int winningPlayers = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 11; j++) {
                if (colorCount[i, j] > i) {
                    winningPlayers++;
                    break;
                }
            }
        }
        return winningPlayers;
    }

    static void Main() {
        int[][] pick = { new int[] {0, 0}, new int[] {1, 0}, new int[] {1, 0}, new int[] {2, 1}, new int[] {2, 1}, new int[] {2, 0} };
        Console.WriteLine(FindWinningPlayersV2(4, pick)); // Output: 2
    }
}

This solution predefines a fixed-size array to count ball colors for each player based on constraints. This keeps memory usage predictable and direct, by leveraging the small number of players and colors specified in the problem constraints.

Complexity

Time Complexity: O(p), where p is the number of picks.

Space Complexity: O(n * 10) = O(n), due to known constraints with up to 11 colors and n players.

Try this approach in the editor

Approach 3: Counting

We can use a 2D array cnt to record the number of balls of each color obtained by each player, and a hash table s to record the IDs of the winning players.

Traverse the pick array, for each element [x, y], we increment cnt[x][y] by one. If cnt[x][y] is greater than x, we add x to the hash table s.

Finally, return the size of the hash table s.

The time complexity is O(m + n times M), and the space complexity is O(n times M). Here, m is the length of the pick array, and n and M are the number of players and the number of colors, respectively.

function winningPlayerCount(n: number, pick: number[][]): number {
    const cnt: number[][] = Array.from({ length: n }, () => Array(11).fill(0));
    const s = new Set<number>();
    for (const [x, y] of pick) {
        if (++cnt[x][y] > x) {
            s.add(x);
        }
    }
    return s.size;
}
func winningPlayerCount(n int, pick [][]int) int {
	cnt := make([][11]int, n)
	s := map[int]struct{}{}
	for _, p := range pick {
		x, y := p[0], p[1]
		cnt[x][y]++
		if cnt[x][y] > x {
			s[x] = struct{}{}
		}
	}
	return len(s)
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Hash Map Approach

Time Complexity: O(p) where p is the number of entries in the 'pick' list.

Space Complexity: O(n * c) where n is the number of players and c is the number of colors.

Array Counting Approach

Time Complexity: O(p), where p is the number of picks.

Space Complexity: O(n * 10) = O(n), due to known constraints with up to 11 colors and n players.

Counting–

Complexity analysis

ApproachTimeSpaceWhen to Use
Hash Map CountingO(m)O(m)General case where color range is unknown or large
Array CountingO(m)O(n * c)When the color range is small and direct indexing is possible

Frequently asked questions

Is Find the Number of Winning Players easy or hard?

Find the Number of Winning Players is classified as an Easy problem. It mainly tests understanding of hash maps, frequency counting, and simple conditional checks rather than complex algorithms.

Find the Number of Winning Players Python/Java solution

In Python or Java, iterate through the picks list and update a hash map keyed by (player, color). When the count exceeds the player index, mark that player as a winner. FleetCode provides full implementations in Python, Java, C++, JavaScript, C, and C#.

How to solve Find the Number of Winning Players in O(n)?

Iterate through the picks array once while maintaining frequency counts of (player, color). When the count for a pair becomes greater than the player index, record that player as a winner using a set or boolean array. This single pass produces an O(m) time solution with constant‑time updates.

What is the best approach for Find the Number of Winning Players?

The hash map counting approach is the most flexible solution. It tracks the number of balls for each (player, color) pair and checks when the count exceeds the player's index. This runs in O(m) time where m is the number of picks and works even when color values are large.

Is Find the Number of Winning Players asked at Google/Amazon/Meta?

Problems based on frequency counting with hash maps and arrays commonly appear in coding interviews at companies like Amazon, Google, and Meta. While this exact problem may vary, the underlying pattern of tracking counts and applying thresholds is frequently tested.

What data structure is used in Find the Number of Winning Players?

The most common data structures are a hash map for frequency counting and a set or boolean array to track players who already won. An alternative implementation uses a 2D array where indices represent players and colors.

What is the time complexity of Find the Number of Winning Players?

The optimal solution runs in O(m) time, where m is the number of picks in the input. Each pick updates a frequency counter and performs a constant‑time comparison. Space complexity is O(m) for the hash map or O(n * c) when using an array counting structure.

Comments

Now write it yourselfClose this page and solve Find the Number of Winning Players in the editor, without looking.
Practise Find the Number of Winning Players