2225.找出输掉零场或一场比赛的玩家
链接:2225.找出输掉零场或一场比赛的玩家
难度:Medium
标签:数组、哈希表、计数、排序
简介:给你一个整数数组 matches 其中 matches[i] = [winneri, loseri] 表示在一场比赛中 winneri 击败了 loseri 。返回一个长度为 2 的列表 answer。
题解 1 - python
- 编辑时间:2024-05-22
- 执行用时:209ms
- 内存消耗:56.78MB
- 编程语言:python
- 解法介绍:遍历。
class Solution:
            def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
                sets = [set() for _ in range(3)]
                for w, l in matches:
                    if w not in sets[0] and w not in sets[1] and w not in sets[2]:
                        sets[0].add(w)
                    if l not in sets[0] and l not in sets[1] and l not in sets[2]:
                        sets[1].add(l)
                    else:
                        idx = 0
                        while idx < 2:
                            if l in sets[idx]:
                                sets[idx].remove(l)
                                sets[idx + 1].add(l)
                                break
                            idx += 1
                return [sorted(v) for v in sets[:2]]
题解 2 - python
- 编辑时间:2024-05-22
- 执行用时:378ms
- 内存消耗:59.18MB
- 编程语言:python
- 解法介绍:遍历。
class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        counter = Counter()
        for w, l in matches:
            if not counter[w]: counter[w] = 0
            counter[l] += 1
        res = [[], []] 
        for k, v in sorted(counter.items()):
            if v < 2:
                res[v].append(k)
        return res