|
1 | 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]); |
| 2 | + public List<List<Integer>> findWinners(int[][] matches) { |
| 3 | + Map<Integer, Integer> playerToLossCount = new HashMap<>(); |
| 4 | + for (int[] match : matches) { |
| 5 | + int winner = match[0]; |
| 6 | + int loser = match[1]; |
| 7 | + playerToLossCount.putIfAbsent(winner, 0); |
| 8 | + playerToLossCount.put(loser, playerToLossCount.getOrDefault(loser, 0) + 1); |
| 9 | + } |
| 10 | + List<List<Integer>> result = new ArrayList<>( |
| 11 | + Arrays.asList(new ArrayList<>(), new ArrayList<>()) |
| 12 | + ); |
| 13 | + for (Map.Entry<Integer, Integer> entry : playerToLossCount.entrySet()) { |
| 14 | + if (entry.getValue() <= 1) { |
| 15 | + result.get(entry.getValue()).add(entry.getKey()); |
| 16 | + } |
| 17 | + } |
| 18 | + Collections.sort(result.get(0)); |
| 19 | + Collections.sort(result.get(1)); |
| 20 | + return result; |
9 | 21 | }
|
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 | 22 | }
|
0 commit comments