---
# System prepended metadata

title: 'Leetcode [No. 2225] Find Players With Zero or One Losses (MEDIUM) 解題心得'
tags: [leetcode]

---

# Leetcode [No. 2225] Find Players With Zero or One Losses (MEDIUM)

2024/1/15 Daily Challenge

## 題目

https://leetcode.com/problems/find-players-with-zero-or-one-losses/description/

## 思路
+ 這個解法就是單純把問題反過來想，既然我只要記得全贏或是輸一次的隊伍
+ 那我不如反過來記就好了?
    + 全贏=輸0次
    + 輸一次=輸一次
    + 所以只要記得看到贏的人就幫在lostCnt+=0 (為了讓Map知道有這個隊伍存在)
+ `unordered_map`在這題不可以使用，因為輸出的順序也會影響到結果
    + ![image](https://hackmd.io/_uploads/r1jIPvGFp.png)
    + 改成`map`就過了
```c++=
class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches) {
        vector<vector<int>> ans(2);
        map<int, int> lostCnt;
        for(int i = 0; i < matches.size(); i++)
        {
            lostCnt[matches[i][0]] += 0;
            lostCnt[matches[i][1]] += 1;
        }
        for(auto& it: lostCnt)
        {
            if(it.second == 0)
            {
                ans[0].emplace_back(it.first);
            }
            else if (it.second == 1)
            {
                ans[1].emplace_back(it.first);
            }
            else
            {
                continue;
            }
            // cout << it.first << ',' << it.second << endl;
        }
        return ans;
    }
};
```

### 解法分析
+ time complexity: O(n)


### 執行結果
![image](https://hackmd.io/_uploads/Sy9OPwGFp.png)

