用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

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

  1. LN : leetcode 406 Queue Reconstruction by Height

    lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...

  2. [LeetCode] 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 ...

  3. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  4. [leetcode] 406. Queue Reconstruction by Height (medium)

    原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...

  5. LC 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 ...

  6. 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 406. Queue Reconstruction by Height

    一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...

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

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

  9. LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46

    406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...

随机推荐

  1. [Python]Codecombat攻略之地牢Kithgard(1-22关)

    首页:https://cn.codecombat.com/play语言:Python 第一界面:地牢 Kithgard(22关) 时间:1-3小时 内容:语法.方法.参数.字符串.循环.变量等 网页: ...

  2. 使用 Proxy + Promise 实现 依赖收集

    (深入浅出Vue基于“依赖收集”的响应式原理) ,这篇文章讲的是通过一个通俗易懂例子,介绍了 如何用Object.defineProperty 实现的“依赖收集”的原理.Object.definePr ...

  3. 正六边形网格化(Hexagonal Grids)原理与实现

    在路径规划.游戏设计栅格法应用中,正六边形网格不如矩形网格直接和常见,但是正六边形具有自身的应用特点,更适用于一些特殊场景中,比如旷阔的海洋.区域或者太空.本文主要讲述如何对正六边形进行几何学分析.网 ...

  4. typeof、constructor和instance

    在JavaScript中,我们经常使用typeof来判断一个变量的类型,使用格式为:typeof(data)或typeof data.typeof返回的数据类型有六种:number.string.bo ...

  5. ASP.NET Core 开源GitServer 实现自己的GitHub

    ASP.NET Core 2.0 开源Git HTTP Server,实现类似 GitHub.GitLab. GitHub:https://github.com/linezero/GitServer ...

  6. python 带小数点时间格式化

    #获取带小数点的时间>>> import datetime #当前时间加3天 >>> t1 = datetime.datetime.now() + datetime ...

  7. 关于IntelliJ IDEA删除项目

    刚开始使用IDEA . 自己创建项目玩,结果发现IDEA无法删除,我也是醉了,Eclipse直接右键 -> delete -> 勾选删除源文件 就删除了,IDEA死活没有找到删除选项... ...

  8. Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能

    1 环境搭建 1.1 Python安装 1.2 MySQL环境搭建 1.3安装MySQLdb  2 具体实现 2.1 登陆界面 2.2 注册界面 2.3 具体实现部分代码   1 环境搭建 1.1 P ...

  9. Leetcode题解(十)

    29.Divide Two Integers 题目 题目要求不用乘除和取模运算,实现两个整数相除: 我的第一想法就是把除法变成减法来做,这也是最初除法的定义,其实现代码如下: class Soluti ...

  10. Line belt

    Problem Description In a two-dimensional plane there are two line belts, there are two segments AB a ...