LeetCode 题解工作台

找出输掉零场或一场比赛的玩家

给你一个整数数组 matches 其中 matches[i] = [winner i , loser i ] 表示在一场比赛中 winner i 击败了 loser i 。 返回一个长度为 2 的列表 answer : answer[0] 是所有 没有 输掉任何比赛的玩家列表。 answer[1] …

category

4

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们用一个哈希表 记录每个玩家输掉的比赛场次。 然后遍历哈希表,将输掉 场比赛的玩家放入 ,将输掉 场比赛的玩家放入 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 matches 其中 matches[i] = [winneri, loseri] 表示在一场比赛中 winneri 击败了 loseri

返回一个长度为 2 的列表 answer

  • answer[0] 是所有 没有 输掉任何比赛的玩家列表。
  • answer[1] 是所有恰好输掉 一场 比赛的玩家列表。

两个列表中的值都应该按 递增 顺序返回。

注意:

  • 只考虑那些参与 至少一场 比赛的玩家。
  • 生成的测试用例保证 不存在 两场比赛结果 相同

 

示例 1:

输入:matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
输出:[[1,2,10],[4,5,7,8]]
解释:
玩家 1、2 和 10 都没有输掉任何比赛。
玩家 4、5、7 和 8 每个都输掉一场比赛。
玩家 3、6 和 9 每个都输掉两场比赛。
因此,answer[0] = [1,2,10] 和 answer[1] = [4,5,7,8] 。

示例 2:

输入:matches = [[2,3],[1,3],[5,4],[6,4]]
输出:[[1,2,5,6],[]]
解释:
玩家 1、2、5 和 6 都没有输掉任何比赛。
玩家 3 和 4 每个都输掉两场比赛。
因此,answer[0] = [1,2,5,6] 和 answer[1] = [] 。

 

提示:

  • 1 <= matches.length <= 105
  • matches[i].length == 2
  • 1 <= winneri, loseri <= 105
  • winneri != loseri
  • 所有 matches[i] 互不相同
lightbulb

解题思路

方法一:哈希表 + 排序

我们用一个哈希表 cnt\textit{cnt} 记录每个玩家输掉的比赛场次。

然后遍历哈希表,将输掉 00 场比赛的玩家放入 ans[0]\textit{ans}[0],将输掉 11 场比赛的玩家放入 ans[1]\textit{ans}[1]

最后将 ans[0]\textit{ans}[0]ans[1]\textit{ans}[1] 按照升序排序,返回结果。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 为比赛场次数。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        cnt = Counter()
        for winner, loser in matches:
            if winner not in cnt:
                cnt[winner] = 0
            cnt[loser] += 1
        ans = [[], []]
        for x, v in sorted(cnt.items()):
            if v < 2:
                ans[v].append(x)
        return ans
speed

复杂度分析

指标
时间complexity is O(n + k log k) where n is the number of matches and k is the number of unique players, due to array scanning and sorting. Space complexity is O(k) for the hash map storing loss counts.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Expect discussion on hash table versus array-only counting trade-offs.

  • question_mark

    Watch for missed edge cases like players with no matches.

  • question_mark

    Check if candidates sort results before returning to meet output order requirements.

warning

常见陷阱

外企场景
  • error

    Failing to include players who never lost any match in the zero-loss list.

  • error

    Incorrectly counting multiple losses for a player when matches list contains repeated winners or losers.

  • error

    Returning unsorted lists which violates the increasing order requirement.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Find players with exactly k losses instead of zero or one.

  • arrow_right_alt

    Return players grouped by total wins instead of losses.

  • arrow_right_alt

    Handle cases with duplicate matches or missing players in the dataset.

help

常见问题

外企场景

找出输掉零场或一场比赛的玩家题解:数组·哈希·扫描 | LeetCode #2225 中等