2225. Find Players With Zero or One Losses 發表於 2023-02-13 | 分類於 leetcode problemsloution123456789101112131415161718192021222324252627282930313233class 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)