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]]

一开始我的思路是根据k值从小到大排,如果k值相同则h值从大到小排。然后再遍历排序过后的数组,进行按值插入。看过别人的分析后,更好的方式是先根据h值从大到小排,如果h值相同则k值从小到大排。然后将数组按顺序插入到ArrayList中,经过第二种排序方式的数组,当前即将被插入的数组的k值就是它在list中应有的位置。这是因为数组先按照h值从大到小排序过,因此已经在list中的数组的h值都是大于或者等于当前数组的h值的。代码如下:

Queue Reconstruction by Height的更多相关文章

  1. sort学习 - LeetCode #406 Queue Reconstruction by Height

    用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...

  2. LN : leetcode 406 Queue Reconstruction by Height

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

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

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

  4. 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 ...

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

  6. 406. Queue Reconstruction by Height

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

  7. LeetCode_406. Queue Reconstruction by Height解题思路

    题目如下: Suppose you have a random list of people standing in a queue. Each person is described by a pa ...

  8. [Swift]LeetCode406. 根据身高重建队列 | Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  9. LeetCode406. Queue Reconstruction by Height Add to List

    Description Suppose you have a random list of people standing in a queue. Each person is described b ...

随机推荐

  1. 6.1-CALayer 使用

    @设置圆角 注意点 1圆角效果,并不是在给定frame布局后有,要给定内容后才有 //头像 NSData *data = [[DJXMPPTool sharedInstance].cardAvatar ...

  2. 比较评测epoll,select,and poll 时间机制

    https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE21815

  3. Software Engineering: 3. Project planning

    recourse: "Software Engineering", Ian Sommerville Keywords for this chapter: planning sche ...

  4. python3 文件增删查

    #Author by Andy#_*_ coding:utf-8 _*_import sysimport timePath="E:\my python study\\files\haprox ...

  5. 用struts实现简单的登录

    1.建项目时选java EE6.0 2.写登陆界面 <body> <center> <form id="form1" name="form1 ...

  6. adb命令学习

    录制屏幕操作Android4.4版本以上支持录制屏幕 adb shell screenrecord /sdcard/demo.mp4 ADB logcat 输出时间信息: adb logcat -v ...

  7. liunx 系统 git clone ssh代码时需要sshkey

    1. 在root用户目录下 执行命令 cd .ssh cat id_rsa.pub 粘贴 ssh key

  8. css学习笔记 8

    标准盒模型:盒子宽 = marginleft+borderleft+paddingleft+width+paddingright+borderright+marginright; 盒子高=margin ...

  9. Phalapi 中Union和Union All的用法

    有时候在进行数据库查询的时候会用到union查询,但是查询的时候会发现两个limit只有一个是有效的,如下查询 select * from table where status = 0 limit 1 ...

  10. activitygroup下的activity不回调onactivityresult的解决方法

    就是activitygroup下的子activity启动第三方activity的时候需要通过getparent的startactivityforresult方法来启动.getparent其实就是这个a ...