Skip to content

LeetCode: 2225-Find Players With Zero or One Losses 解題紀錄

Last Updated on 2022-11-28 by Clay

題目

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].

Example 2:

Input: matches = [[2,3],[1,3],[5,4],[6,4]]
Output: [[1,2,5,6],[]]
Explanation:
Players 1, 2, 5, and 6 have not lost any matches.
Players 3 and 4 each have lost two matches.
Thus, answer[0] = [1,2,5,6] and answer[1] = [].

Constraints:

  • 1 <= matches.length <= 105
  • matches[i].length == 2
  • 1 <= winneri, loseri <= 105
  • winneri != loseri
  • All matches[i] are unique.

題目輸入一連串的比賽結果 matchesmatches[i][0] 代表第 i 回合的勝者、matches[i][1] 代表第 i 回合的敗者。

最終,我們希望返回 {{完全沒輸過的參賽者}, {只輸過一場的參賽者}} 這樣的結果。需要注意的是,完全沒輸過的參賽者至少也要參加過一次比賽。


解題思路

我這題的解法比較直接,當然 Runtime 的分數就不是很好看... 我直接建立了一個 lossTimes 的雜湊表,key 值是參賽者編號,value 則是輸掉的次數。

當然從來沒參賽的人就不會存在著 key 值,而我們可以透過 value 的累積直接判斷這個參賽者輸過幾場。


C++ 範例程式碼

class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches) {
        // Init
        int highestPlayer = 0;
        unordered_map<int, int> lossTimes;
        vector<vector<int>> results(2);
        
        // Records match results
        for (int i=0; i<matches.size(); ++i) {
            lossTimes[matches[i][0]] += 0;
            lossTimes[matches[i][1]] += 1;
            highestPlayer = max(max(matches[i][0], matches[i][1]), highestPlayer);
        }
        
        // Save to `results`
        for (int i=1; i<=highestPlayer; ++i) {
            if (!lossTimes.count(i)) {
                continue;
            }
            if (lossTimes[i] == 0) {
                results[0].push_back(i);
            }
            else if (lossTimes[i] == 1) {
                results[1].push_back(i);
            }
        }
        
        return results;
    }
};



Python 範例程式碼

class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        # Init
        highest_idx = 0
        loss_times = {}
        results = [[], []]
        
        # Records match results
        for match in matches:
            loss_times[match[0]] = loss_times.get(match[0], 0) + 0
            loss_times[match[1]] = loss_times.get(match[1], 0) + 1
            highest_idx = max(max(match[0], match[1]), highest_idx)
        
        # Save to `results`
        for idx in range(1, highest_idx+1):
            if loss_times.get(idx, -1) == 0:
                results[0].append(idx)
            elif loss_times.get(idx, -1) == 1:
                results[1].append(idx)
        
        return results

References


Read More

Leave a Reply取消回覆

Exit mobile version