2225. Find Players With Zero or One Losses

problem

sloution

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
class Solution {
public:
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
map<int,pair<int,int>> mp;
vector<int> winners, losers;
for(auto m:matches){
int win = m[0], lose = m[1];
if(mp.count(win)){
pair<int,int> &p= mp[win];
p.first+=1;
}
else mp[win] = {1,0};
if(mp.count(lose)){
pair<int,int> &p= mp[lose];
p.second+=1;
}
else mp[lose] = {0,1};
}
for(auto m:mp){
// winner
if(m.second.second ==0 && m.second.first > 0){
winners.push_back(m.first);
}
// loser == 1
if(m.second.second == 1){
losers.push_back(m.first);
}
}
return {winners, losers};


}
};

analysis

  • time complexity O(nlogn)
  • space complexity O(n)