LC.P2225[找出输掉零场或一场比赛的玩家]

方法一:哈希表+排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer, Integer> map = new HashMap<>();
for (int[] m : matches) {
int a = m[0], b = m[1];
map.putIfAbsent(a, 0);
map.merge(b, 1, Integer::sum);
}
List<Integer> a = new ArrayList<>(), b = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int k = entry.getKey(), v = entry.getValue();
if (v == 0) a.add(k);
else if (v == 1) b.add(k);
}
Collections.sort(a);
Collections.sort(b);
return List.of(a, b);
}
}
  • 时间复杂度:$O(nlogn)$
  • 空间复杂度:$O(n)$