[LeetCode] Find Players With Zero or One Losses

2225. Find Players With Zero or One Losses

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
unordered_map<int, int> mp;
for(auto& m : matches) {
mp[m[1]]++;
mp[m[0]] += 0;
}
vector<vector<int>> res(2);

for(auto [p, l] : mp) {
if(l == 0) res[0].push_back(p);
else if(l == 1) res[1].push_back(p);
}

sort(res[0].begin(), res[0].end());
sort(res[1].begin(), res[1].end());
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/03/PS/LeetCode/find-players-with-zero-or-one-losses/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.