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
sort学习 - LeetCode #406 Queue Reconstruction by Height的更多相关文章
- LN : leetcode 406 Queue Reconstruction by Height
lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...
- [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 ...
- [leetcode] 406. Queue Reconstruction by Height
https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...
- [leetcode] 406. Queue Reconstruction by Height (medium)
原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...
- 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 ...
- 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 406. Queue Reconstruction by Height
一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...
- 406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
随机推荐
- Windows系统下使用Jenkins 自动发布 .NET core到Linux平台下Docker
准备工作(安装过程可以百度,已安装的可以跳过) a) 安装Jenkins,安装包下载地址:http://mirrors.tuna.tsinghua.edu.cn/jenkins/windows ...
- Arrays.asList () 不可添加或删除元素的原因
Java中奖数组转换为List<T>容器有一个很方便的方法 Arrays.asList(T ... a),我通过此方法给容器进行了赋值操作,接着对其进行 添加元素,却发现会抛出一个(jav ...
- 【转】DMA和cache一致性
DMA和cache一致性问题 Cache原理 CPU缓存(Cache Memory)是位于CPU与内存之间的临时存储器,它的容量比内存小的多但是交换速度却比内存要快得多.缓存的出现主要是为了解决CPU ...
- phalcon——访问控制列表ACL
一个完整的使用实例(将acl封装成一个插件使用): use Phalcon\Acl; use Phalcon\Acl\Role; use Phalcon\Acl\Resource; use Phalc ...
- LeetCode 624. Maximum Distance in Arrays (在数组中的最大距离)$
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...
- LAMP 实现全过程及wordpress的搭建
一.介绍 1. LAM(M)P: L:linux A:apache (httpd) M:mysql, mariadb M:memcached 缓存 P:php, perl, python WEB 资源 ...
- Python迭代
本篇将介绍Python的迭代,更多内容请参考:Python学习指南 简介 在Python中,如果给定一个list或者tuple,我们可以通过for循环来遍历这个list或者tuple,这种遍历我们称为 ...
- windows 系统下C++实现的多线程
摘抄http://blog.csdn.net/huyiyang2010/article/details/5809919 Thread.h #ifndef __THREAD_H__ #define __ ...
- Just a Hook(区间set)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Python中编码问题:u'\xe6\x97\xa0\xe5\x90\x8d' 类型的转为utf-8的解决办法
相信小伙伴们遇到过类似这样的问题,python2中各种头疼的转码,类似u'\xe6\x97\xa0\xe5\x90\x8d' 的编码,直接s.decode()是无法解决编码问题.尝试了无数办法,都无法 ...