用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. Xamarin Forms 进度条控件

    本文翻译:http://xamlnative.com/2016/04/14/xamarin-forms-a-simple-circular-progress-control/ 里面都是胡说的,如果看不 ...

  2. 【网络爬虫入门05】分布式文件存储数据库MongoDB的基本操作与爬虫应用

    [网络爬虫入门05]分布式文件存储数据库MongoDB的基本操作与爬虫应用 广东职业技术学院  欧浩源 1.引言 网络爬虫往往需要将大量的数据存储到数据库中,常用的有MySQL.MongoDB和Red ...

  3. JavaScript函数之作用域 / 作用链域 / 预解析

    关于作用域和作用链域的问题,很多文章讲的都很详细,本文属于摘录自己觉得对自己有价值的部分,留由后用,仅供参考,需要查看详细信息请点击我给出的原文链接查看原文件 做一个有爱的搬运工~~ -------- ...

  4. WordPress 4.8 安装配置教程 (基于 centos 7.3, php 7.0, mysql 5.7.19, nginx 1.12.1)

    最近想要整个 blog,记录自己工作.学习中的点滴.Wordpress 自然是首选,因为内容才是关键,所以也就不怕别人说太 low.网上大部份都是讲 wordpress 配合 apache 的安装教程 ...

  5. Git相关操作四

    1.克隆远程仓库 git clone remote_location clone_name remote_location为仓库地址,clone_name为要克隆到本地的仓库名称. 2.显示对应克隆地 ...

  6. MySql sql按时间分组

    select DATE_FORMAT(f.upload_time,'%Y%u') weeks,count(*),sum(p.download_times),sum(p.collection_times ...

  7. PHP备忘录

    file_exists()在判断文件是否存在的时候是递归判断每个目录是不是有执行权限. Echo输出大字符串速度慢:打开apache配置项‘deflate’进行压缩输出.

  8. 虚拟专用网VPN

    1. 三个专用地址块: (1)10.0.0.0到10.255.255.255 (2)172.16.0.0到172.31.255.255 (3)192.168.0.0到192.168.255.255 2 ...

  9. LeetCode 136. Single Number (落单的数)

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  10. angularJs 个人初探笔记

    1.环境搭建与angular - phoneCat 安装可以通过git clone来下载源代码: git clone --depth=14 https://github.com/angular/ang ...