Skip to content

Instantly share code, notes, and snippets.

@dluciano
Created November 28, 2022 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dluciano/48d4e48701869d631a63af1b04397692 to your computer and use it in GitHub Desktop.
Save dluciano/48d4e48701869d631a63af1b04397692 to your computer and use it in GitHub Desktop.
2225. Find Players With Zero or One Losses
public class Solution {
public IList<IList<int>> FindWinners(int[][] matches) {
var winnerDict = new Dictionary<int, int>();
var loserDict = new Dictionary<int, int>();
foreach(var m in matches){
var winner = m[0];
var loser = m[1];
if(!winnerDict.ContainsKey(winner))
winnerDict.Add(winner, 0);
if(!loserDict.ContainsKey(loser))
loserDict.Add(loser, 0);
winnerDict[winner]++;
loserDict[loser]++;
}
var winnersWithNoLost = winnerDict.Where(w=> !loserDict.ContainsKey(w.Key)).Select(w => w.Key).OrderBy(w => w).ToList();
var exactlyOneLost = loserDict.Where(l => l.Value == 1).Select(l => l.Key).OrderBy(l => l).ToList();
var ans = new List<IList<int>>();
ans.Add(winnersWithNoLost);
ans.Add(exactlyOneLost);
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment