Leetcode 406. Queue Reconstruction by Height

巧妙的一道题。
思路:
先把所有people按照个头高低排队,在此基础上再安第二个参数由小到大。
然后遍历上述排序后队伍,先把个子最高的都放进去,然后再继续把小的往里插。插的位置就是第二个参数。
python排序用法:
aftersort = sort(list,key = lambda x:(-x[0], x[1]))

python insert用法:
list.insert(index, item)
代码:

class Solution:
    def reconstructQueue(self, people):
        """
        :type people: List[List[int]]
        :rtype: List[List[int]]
        """
        if not people:
            return []
        res = []
        sort_people = sorted(people, key=lambda x: (-x[0], x[1]))
        max_height = sort_people[0][0]
        for people in sort_people:
            if people[0] == max_height:
                res.append(people)
            else:
                res.insert(people[1], people)
        return res
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容