leetcode力扣406. 根据身高重建队列

假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。

注意:
总人数少于1100人。

示例

输入:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

输出:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

用到了list的sort方法,要熟悉sort方法的一些用法比较好做,这个关键就是先排高的,然后按照k值在k位置插入矮的,以此类推

class Solution(object):
    def reconstructQueue(self, people):
        """
        :type people: List[List[int]]
        :rtype: List[List[int]]
        """
        def sortkey(x):
            return -x[0],x[1]
        people.sort(key = sortkey)
        res = []
        for p in people:
            res.insert(p[1], p)
        return res
发布了332 篇原创文章 · 获赞 170 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/qq_32146369/article/details/104127349