LeetCode 406 Queue Reconstruction by Height
#LeetCode 406. Queue Reconstruction by Height
原题链接:
Queue Reconstruction by Height
###题目大意:
有一个由[h,k]组成的随机的数组如下案例:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
h表示高度,k表示在此之前,大于或等于此高度的元素数量。
要求排序为符合题目的数列,上述案例结果如下所示:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
###解题思路:
很简单的排序,但是有个很精巧的机关。
首先按照高度从大到小排序,相同的高度按照位置从小到大排序
然后从第一个开始插入到一个新数组中。此时很"凑巧"的,第二个k就是应该插入的位置。因为根据之前的排序,当前,新数组的所有元素都大于等于旧数组。
###AC代码:
/*** 406. Queue Reconstruction by Height* @param peoples* @return*/public static int[][] reconstructQueue (int[][] peoples){Arrays.parallelSort(peoples, (a, b) -> a[0] != b[0] ? b[0] - a[0] : a[1] - b[1]);List<int[]> result = new ArrayList<>();for(int i=0;i<peoples.length;i++){result.add(peoples[i][1],peoples[i]);}return result.toArray(new int[0][0]);}
复制代码
LeetCode 406 Queue Reconstruction by Height相关推荐
- LeetCode 406. Queue Reconstruction by Height 题解(C++)
LeetCode 406. Queue Reconstruction by Height 题解(C++) 题目描述 Suppose you have a random list of people s ...
- LeetCode 406. Queue Reconstruction by Height
原题链接在这里:https://leetcode.com/problems/queue-reconstruction-by-height/description/ 题目: Suppose you ha ...
- [Leetcode] 406. Queue Reconstruction by Height 直觉解释
https://leetcode.com/problems/queue-reconstruction-by-height/ 首先,我们找到最小的 height (h, k),这个数据对的最终位置应该是 ...
- leetcode 406. Queue Reconstruction by Height | 406. 根据身高重建队列(Java)
题目 https://leetcode.com/problems/queue-reconstruction-by-height/ 题解 思路:先排序,后插入. 看到 答案 中的一句话,恍然大悟了: 第 ...
- 406. Queue Reconstruction by Height
这题本质上是贪心法, 一个原则 "只有比你身高高的或者相等的才对你有影响,插入任何比你小的人都对你没影响" 因此从大到小排列,先处理大的,再处理小的.每次处理时K 就是插入的位置, ...
- LeetCode Queue Reconstruction by Height(贪心)
问题:给出一个数组,数组中的元素有两个成员,一个表示高度,一个表示在它前面不小于它的个数,要求根据高度重建 思路:如果高度不相等,按高度降序,否则按个数升序 具体代码参考: https://githu ...
- leetcode算法题--Queue Reconstruction by Height
原题链接:https://leetcode.com/problems/queue-reconstruction-by-height/ bool cmp(vector<int> a, vec ...
- 30/100. Queue Reconstruction by Height
假设有一个随机的排队的人的列表.每个人都由一对整数 (h, k) 来描述,其中h是人的身高,k是这个人前面身高大于或等于h的人数.要求编写一个算法来重建队列. 首先将身高从高到底排列,对排好序的sor ...
- leetcode_middle_2_406. Queue Reconstruction by Height
题意: 一个二维数组,表示一列人,每个人用一个[h, k]数组描述,h表示这个人的身高,k表示队列前面身高大于等于这个人的数目. 分析: 比如: [[5,0], [7,0], [5,2], [6,1] ...
最新文章
- libmemcached 1.0.11 发布
- SpringMVC js如何获取后台model中的属性值
- 世界上最好的光刻机为什么来自荷兰?【物联网智商精选】
- C语言试题二十之利用以下的简单迭代方法求方程cos(x)-x=0的一个实根。
- 50万数据生成6位数不重复字符串_R语言系列3:高级数据管理
- 计算机中级职称报考入口,2020年9月计算机职称考试报名流程(附入口)
- line-height:1.5和line-height:150%的区别
- 手写数字识别中多元分类原理_广告行业中那些趣事系列:从理论到实战BERT知识蒸馏...
- MyBatis框架笔记03:MyBatis实现CRUD
- java多线程通信方法
- 使用c++制作微服务计算服务
- RedHat6.4安装IBM MQ7.5
- 银行计算机系统(第3版),清华大学出版社-图书详情-《银行计算机系统》
- 编程基本功:即使是脚本,也尽量减少硬代码,都使用变量
- Windows如何重装系统
- Cortex-M4-SPI总线-读写W25Q64(二)
- mac dmg包签名及公证
- Python:Django面试题
- ImageOptim使用教程之图片压缩的方法
- Struts1.x使用入门