-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind-Players-With-Zero-or-One-Losses.java
50 lines (39 loc) · 1.75 KB
/
Find-Players-With-Zero-or-One-Losses.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
// create empty solution 2D array
List<List<Integer>> result = new ArrayList<>();
// define set for all players , winners and losers
Set<Integer> players = new HashSet<>();
Set<Integer> winners = new HashSet<>();
Set<Integer> losers = new HashSet<>();
Map<Integer,Integer> lostUpdate = new HashMap<>(); // it will update loosecount
Set<Integer> lostOnly1 = new HashSet<>(); // keeps track of only 1 match lost plyr
for(int[] m: matches){
players.add(m[0]); // add match winner
players.add(m[1]); // add match loser
losers.add(m[1]); // add match loser to losers list
int lostCount = lostUpdate.getOrDefault(m[1],0); // if lost then make it 1
lostUpdate.put(m[1],lostCount+1); // if lost again, increment lost count
}
for(int p : players){
if(!losers.contains(p)){ // if player not in losers
winners.add(p);
}
}
for(Map.Entry<Integer,Integer> e : lostUpdate.entrySet()){ // for lost entry
if(e.getValue() == 1){ // if lost count is 1
lostOnly1.add(e.getKey());
}
}
// Create winner List and lost only one list
List<Integer> allWinners = new ArrayList<>(winners);
List<Integer> listLost1 = new ArrayList<>(lostOnly1);
// answer should be in increasing order
Collections.sort(allWinners);
Collections.sort(listLost1);
// add these two lists to final result 2D Array
result.add(new ArrayList<>(allWinners));
result.add(listLost1);
return result;
}
}