Skip to content

Commit 30b1345

Browse files
authored
Create Find Players With Zero or One Losses.java
1 parent 6f4684b commit 30b1345

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public List<List<Integer>> findWinners(int[][] matches) {
3+
Map<Integer, Integer> lossCount = new HashMap<>();
4+
Set<Integer> players = new HashSet<>();
5+
for (int[] match : matches) {
6+
lossCount.put(match[1], lossCount.getOrDefault(match[1], 0) + 1);
7+
players.add(match[0]);
8+
players.add(match[1]);
9+
}
10+
List<Integer> noLoss = new ArrayList<>();
11+
List<Integer> exactlyOneLoss = new ArrayList<>();
12+
for (Integer player : players) {
13+
if (!lossCount.containsKey(player)) {
14+
noLoss.add(player);
15+
}
16+
if (lossCount.getOrDefault(player, 0) == 1) {
17+
exactlyOneLoss.add(player);
18+
}
19+
}
20+
Collections.sort(noLoss);
21+
Collections.sort(exactlyOneLoss);
22+
return Arrays.asList(noLoss, exactlyOneLoss);
23+
}
24+
}

0 commit comments

Comments
 (0)