sort学习 - LeetCode #406 Queue Reconstruction by Height

用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可

但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp;而使用key和reverse就快得多,因为只需要在排序开始时处理一次即可,因此在排序的时候能不用cmp就尽量不用

另外可以用operator函数中的itemgetter,和attrgetter实现基于key的(多级)排序:

from operator import itemgetter, attrgetter
sorted(people, key=itemgetter(0,1))
sorted(people, key=attrgetter(‘h‘,‘k‘))

itemgetter通过属性的下标指定key,attrgetter通过属性名指定key

趁热打铁,用sort实现一下 LeetCode #406 Queue Reconstruction by Height

https://leetcode.com/problems/queue-reconstruction-by-height/

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.

Note:
The number of people is less than 1,100.

Example

Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

基本思路很简单,对people进行排序,h越大k越小的排越前,然后按顺序先给个子高的排位置,再给个子矮的排

因为个子高的位置排好后,再怎么对个子矮的排,都不会影响个子高的人的相对位置

这里的排序涉及两个key,所以要用多级排序

先用传统的cmp方法实现

def reconstructQueue(self, people):
  def my_cmp(p1,p2):
        return cmp(p2[1],p1[1]) if p1[0]==p2[0] else cmp(p1[0],p2[0])

    people.sort(cmp = my_cmp,reverse = True)
    ans = []
    for peo in people:
        ans.insert(peo[1],peo)
    return ans

耗时情况

现在改用基于key实现多级排序,由于h是降序,k是升序,所以暂时还不知道怎么用itemgetter实现,只会自己写一个my_key =  =、

由于sort不reverse,所以my_key中返回的是负的h(people[0])和正的k(people[1])来分别实现降序和升序

def reconstructQueue(self, people):
    def my_key(people):
        return -people[0],people[1]

    people.sort(key=my_key)
    ans = []
    for peo in people:
        ans.insert(peo[1],peo)
    return ans

耗时情况

可以看出sort基于key时在速度上确实快于基于cmp

时间: 2024-11-03 22:30:27

sort学习 - LeetCode #406 Queue Reconstruction by Height的相关文章

406. Queue Reconstruction by Height

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to

406 Queue Reconstruction by Height 根据身高重建队列

假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(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]] 详见:https://leetcode.com/problems/queue-reconstruction-by-h

[LeetCode] Queue Reconstruction by Height 根据高度重建队列

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers(h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h

LeetCode Queue Reconstruction by Height

原题链接在这里:https://leetcode.com/problems/queue-reconstruction-by-height/description/ 题目: Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is th

Queue Reconstruction by Height

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to 

451.Queue Reconstruction by Height (Medium)

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to 

std::sort 学习:一种递归分治方法

// std::sort 学习:一种递归分治方法 今天看了看 stl 的 std::sort 的代码,众所周知,这个函数是在快速排序递归太深的时候使用堆排序防止过度退化,但是今天说的不是这个.我们只看快速排序的部分. 我们一般实现快速排序大概是这样的(本王随意写了个用下标当参数的排序函数,领会意思即可). void quick_sort(int first, int last) // 某个数组的 [first, last) {  if ((last - first) > 1) {  int mi

Sort Colors leetcode java

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, an

Sort List leetcode java

题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和constant space complexity来sort list,自然而然想到了merge sort方法.同时我们还已经做过了merge k sorted list和merge 2 sorted list.这样这个问题就比较容易了. 不过这道题要找linkedlist中点,那当然就要用最经典的