-
Notifications
You must be signed in to change notification settings - Fork 0
/
2225. Find Players With Zero or One Losses
166 lines (142 loc) · 5.38 KB
/
2225. Find Players With Zero or One Losses
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
Return a list answer of size 2 where:
answer[0] is a list of all players that have not lost any matches.
answer[1] is a list of all players that have lost exactly one match.
The values in the two lists should be returned in increasing order.
Note:
You should only consider the players that have played at least one match.
The testcases will be generated such that no two matches will have the same outcome.
Example 1:
Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
Output: [[1,2,10],[4,5,7,8]]
Explanation:
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
//approach-1
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
//this is my code
int n= matches.length;
List<List<Integer>> res=new ArrayList<>();
Set<Integer> players=new HashSet<>();
for(int i=0;i<n;i++){
for(int j=0;j<2;j++){
if(!players.contains(matches[i][j]))
players.add(matches[i][j]);
}
}
//store the loses of each player
HashMap<Integer,Integer> count_loss=new HashMap<>();
for(int i=0;i<n;i++){
if(!count_loss.containsKey(matches[i][1]))
count_loss.put(matches[i][1],1);
else
count_loss.put(matches[i][1],count_loss.get(matches[i][1])+1);
}
List<Integer> one_loss=new ArrayList<>();
for(Map.Entry<Integer,Integer> entry: count_loss.entrySet()){
int key=entry.getKey();
int loss= entry.getValue();
if(loss==1)
one_loss.add(key);
}
List<Integer> no_loss=new ArrayList<>();
for(int x:players){
if(!count_loss.containsKey(x))
no_loss.add(x);
}
Collections.sort(no_loss);
Collections.sort(one_loss);
res.add(no_loss);
res.add(one_loss);
return res;
}
}
Approach 2: Hash Set
Time complexity: O(n\cdot \log n)O(n⋅logn)
space com= O(n)
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Set<Integer> zeroLoss = new HashSet<>(), oneLoss = new HashSet<>(),\
moreLosses = new HashSet<>();
for (int[] match : matches) {
int winner = match[0], loser = match[1];
// Add winner.
if (!oneLoss.contains(winner) && !moreLosses.contains(winner)) {
zeroLoss.add(winner);
}
// Add or move loser.
if (zeroLoss.contains(loser)) {
zeroLoss.remove(loser);
oneLoss.add(loser);
} else if (oneLoss.contains(loser)) {
oneLoss.remove(loser);
moreLosses.add(loser);
} else if (moreLosses.contains(loser)) {
continue;
} else {
oneLoss.add(loser);
}
}
List<List<Integer>> answer =
Arrays.asList(new ArrayList<>(), new ArrayList<>());
answer.get(0).addAll(zeroLoss);
answer.get(1).addAll(oneLoss);
Collections.sort(answer.get(0));
Collections.sort(answer.get(1));
return answer;
}
}
//Approach 2: Hash Set + Hash Map
Time complexity: O(n\cdot \log n)O(n⋅logn)
space com= O(n)
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Set<Integer> seen = new HashSet<>();
Map<Integer, Integer> lossesCount = new HashMap<>();
for (int[] match : matches) {
int winner = match[0], loser = match[1];
seen.add(winner);
seen.add(loser);
lossesCount.put(loser, lossesCount.getOrDefault(loser, 0) + 1);
}
// Add players with 0 or 1 loss to the corresponding list.
List<List<Integer>> answer =
Arrays.asList(new ArrayList<>(), new ArrayList<>());
for (int player : seen) {
if (!lossesCount.containsKey(player)) {
answer.get(0).add(player);
} else if (lossesCount.get(player) == 1) {
answer.get(1).add(player);
}
}
Collections.sort(answer.get(0));
Collections.sort(answer.get(1));
return answer;
}
}
//Approach 3: Hash Map
Time complexity: O(n\cdot \log n)O(n⋅logn)
space com= O(n)
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer, Integer> lossesCount = new HashMap<>();
for (int[] match : matches) {
int winner = match[0], loser = match[1];
lossesCount.put(winner, lossesCount.getOrDefault(winner, 0));
lossesCount.put(loser, lossesCount.getOrDefault(loser, 0) + 1);
}
List<List<Integer>> answer = Arrays.asList(new ArrayList<>(), new ArrayList<>());
for (Integer player : lossesCount.keySet())
if (lossesCount.get(player) == 0) {
answer.get(0).add(player);
} else if (lossesCount.get(player) == 1) {
answer.get(1).add(player);
}
Collections.sort(answer.get(0));
Collections.sort(answer.get(1));
return answer;
}
}