哈希加排序,LeetCode 2225. 找出输掉零场或一场比赛的玩家

一、题目

1、题目描述

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

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

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

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

注意:

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

2、输入输出

2.1输入
输入:matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
2.2输出

输出:[[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] 。

3、原题链接

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


二、解题报告

1、思路分析

记录选手编号集合,以及每个选手输的次数

将输的次数为1的放一块,没输过的放一块,排序返回

2、复杂度

时间复杂度: O(N)空间复杂度:O(N)

3、代码详解

​py3
class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        cnt = Counter(x for _, x in matches)
        st = set(x for m in matches for x in m)
        return [sorted(x for x in st if cnt[x] == 0), sorted(x for x in st if cnt[x] == 1)]
cpp
class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches) {
        unordered_map<int, int> cnt;
        set<int> st;
        for (auto& v : matches) {
            int x = v[0], y = v[1];
            cnt[y] ++;
            st.insert(x), st.insert(y);
        }
        vector<vector<int>> ret(2, vector<int>());
        for (int x : st) 
            if (cnt[x] == 1) ret[1].push_back(x);
            else if(cnt[x] == 0) ret[0].push_back(x);
        return ret;
    }
};

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值