-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindPlayersWithZeroOrOneLosses.java
More file actions
37 lines (31 loc) · 1.06 KB
/
FindPlayersWithZeroOrOneLosses.java
File metadata and controls
37 lines (31 loc) · 1.06 KB
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
29
30
31
32
33
34
35
36
37
package array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* Description: https://leetcode.com/problems/find-players-with-zero-or-one-losses
* Difficulty: Medium
* Time complexity: O(nlog n)
* Space complexity: O(n)
*/
public class FindPlayersWithZeroOrOneLosses {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer, Integer> playerToLosses = new TreeMap<>();
for (int[] match : matches) {
playerToLosses.putIfAbsent(match[0], 0);
playerToLosses.merge(match[1], 1, Integer::sum);
}
List<List<Integer>> winners = List.of(new ArrayList<>(), new ArrayList<>());
for (Map.Entry<Integer, Integer> entry : playerToLosses.entrySet()) {
int player = entry.getKey();
int losses = entry.getValue();
if (losses == 0) {
winners.get(0).add(player);
} else if (losses == 1) {
winners.get(1).add(player);
}
}
return winners;
}
}