2225. Find Players With Zero or One Losses
MediumView on LeetCode
Time: O(n)
Space: O(n)
Problem Overview
Count wins per team from match results.
Advertisement
Intuition
Count wins per team from match results. Track frequency in a map; find maximum win count; return all teams tied at that maximum (sorted by index or name per problem).
Algorithm
- 1Initialize wins map.
- 2For each match [winner, loser]: wins[winner]++.
- 3Find maxWin = max of map values.
- 4Collect all teams with wins[team] == maxWin into result list.
- 5Return result (often sorted by team index).
Example Walkthrough
Input: matches with Alice/Bob wins
- 1.Tally wins per name.
- 2.Return every team with the highest tally.
Output: list of top teams
Common Pitfalls
- •Losers are not inserted unless they win elsewhere.
- •Ties return multiple teams.
- •Team ids may be integers — map accordingly.
2225.cs
C#
// Approach: HashMap tracking loss counts; separate players with 0 and exactly 1 loss.
// Time: O(n) Space: O(n)
public class Solution
{
public IList<IList<int>> FindWinners(int[][] matches)
{
var wmap = new Dictionary<int, int>();
var lmap = new Dictionary<int, int>();
foreach (int[] match in matches)
{
int winner = match[0];
int loser = match[1];
if (wmap.ContainsKey(winner))
wmap[winner]++;
else
wmap.Add(winner, 1);
if (lmap.ContainsKey(loser))
lmap[loser]++;
else
lmap.Add(loser, 1);
}
var lostOnlyOne = new List<int>();
foreach (var kvp in lmap)
{
if (kvp.Value == 1)
lostOnlyOne.Add(kvp.Key);
}
lostOnlyOne.Sort();
var neverLost = new List<int>();
foreach (var kvp in wmap)
{
if (!lmap.ContainsKey(kvp.Key))
neverLost.Add(kvp.Key);
}
neverLost.Sort();
var ans = new List<IList<int>>();
ans.Add(neverLost);
ans.Add(lostOnlyOne);
return ans;
}
}Advertisement
Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)