Created
November 28, 2022 11:12
-
-
Save dluciano/48d4e48701869d631a63af1b04397692 to your computer and use it in GitHub Desktop.
2225. Find Players With Zero or One Losses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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