美文网首页
leetcode 870 优势洗牌

leetcode 870 优势洗牌

作者: Arsenal4ever | 来源:发表于2020-01-24 22:17 被阅读0次

这题和田忌赛马一样,但卡了我好久,难在了数据结构的设计。如果只用字典保留原来的顺序,值相同则会错误,要用字典+列表组合的方式。

class Solution(object):
    def advantageCount(self, A, B):
        """
        :type A: List[int]
        :type B: List[int]
        :rtype: List[int]
        """
        assigned={b:[] for b in B}
        sortedB, sortedA, remaining = sorted(B),sorted(A),[]
        j = 0
        for a in sortedA:
            if a > sortedB[j]:
                assigned[sortedB[j]].append(a)
                j+=1
            else:
                remaining.append(a)
        return [assigned[b].pop() if assigned[b] else remaining.pop() for b in B]

相关文章

  • LeetCode每日一题:田忌赛马(最大胜场数)

    870. 优势洗牌[https://leetcode.cn/problems/advantage-shuffle/...

  • leetcode 870 优势洗牌

    这题和田忌赛马一样,但卡了我好久,难在了数据结构的设计。如果只用字典保留原来的顺序,值相同则会错误,要用字典+列表...

  • LeetCode 870. 优势洗牌

    LeetCode 870. 优势洗牌 给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i...

  • 870. 优势洗牌(Python)

    难度:★★★☆☆类型:数组方法:数学 力扣链接请移步本题传送门[https://leetcode-cn.com/p...

  • Leetcode 870. Advantage Shuffle

    文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. S...

  • Leetcode【392、870、881、1090】

    问题描述:【Greedy】392. Is Subsequence 解题思路: 这道题是给两个字符串 s 和 t,判...

  • Leetcode_384_打乱数组_hn

    题目描述 打乱一个没有重复元素的数组。 示例 解答方法 方法一:洗牌算法 思路 https://leetcode-...

  • 求求你……帮我杀了那个SCP吧—870

    SCP-870 ██████ ████先生在日记中对SCP-870的素描 项目编号:SCP-870 项目等级:Ke...

  • 870

    这几天晟晟学着骑电动车了,个子是可以了,但是电动车属于危险代步工具,初学者很容易出事故,所以不轻易放手,可以...

  • 870

    今天为了在游戏里等人被带飞,早上八点半就醒了。因为他跟我约的是九点,九点到了,他没来,9:30他跟我说改成十点。十...

网友评论

      本文标题:leetcode 870 优势洗牌

      本文链接:https://www.haomeiwen.com/subject/ueqhthtx.html