2225. Find Players With Zero or One Losses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer, Integer> playerToloseCountMap = new HashMap<>();

for (int[] match : matches) {
int winner = match[0], loser = match[1];
if (!playerToloseCountMap.containsKey(winner)) {
playerToloseCountMap.put(winner, 0);
}
playerToloseCountMap.put(loser, playerToloseCountMap.getOrDefault(loser, 0) + 1);
}

List<List<Integer>> resultList = new ArrayList<>();
resultList.add(new ArrayList<>());
resultList.add(new ArrayList<>());

for (Map.Entry<Integer, Integer> entry : playerToloseCountMap.entrySet()) {
int player = entry.getKey(), loseCount = entry.getValue();
if (loseCount < 2) {
resultList.get(loseCount).add(player);
}
}

Collections.sort(resultList.get(0));
Collections.sort(resultList.get(1));
return resultList;
}
}

References

2225. Find Players With Zero or One Losses