LeetCode刷题笔记 406. 根据身高重建队列
2019-06-30 18:59
260 查看
题目描述
假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。
示例:
输入:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
输出:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
总结
先由身高降序排序,k升序排序
然后用类似插入排序的方式得到结果集
Sample & Demo Code
class Solution { public int[][] reconstructQueue(int[][] people) { Arrays.sort(people, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]; } }); // for(int[] i : people) // System.out.println(i[0]+","+i[1]); List<int[]> list = new ArrayList<>(); for(int[] i : people) list.add(i[1], i); return list.toArray(new int[list.size()][]); // return list.toArray(people); } }
相关文章推荐
- Leetcode406. 根据身高重新构造队列
- 406. Queue Reconstruction by Height(根据身高重排队列)
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
- (剑指offer笔记)根据前序遍历和后序遍历重建二叉树
- LeetCode算法题集-406. Queue Reconstruction by Height(通过高重建队列)
- [LeetCode] Queue Reconstruction by Height 身高队列
- [LeetCode] 根据中序和后序序列重建二叉树
- LeetCode笔记:406. Queue Reconstruction by Height
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- [LeetCode] 根据前序序列和中序序列重建二叉树
- leetcode_406. Queue Reconstruction by Height 按身高h和比他高的人的个数k的二元组(h,k) 排队
- [leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树
- 假设你有一个随机的队列列表。 每个人用一对整数(h,k)来描述,其中h是人的身高,k是身高大于或等于h的人前面的人数。 编写一个算法来重建队列。
- 每天一个算法之根据前序中序序列重建二叉树
- leetcode学习笔记24
- 可阻塞的队列-笔记整理12
- LeetCode 刷题笔记总结
- leetcode笔记--Longest Common Prefix
- 199.leetcode Binary Tree Right Side View(medium)[层次遍历二叉树 队列]
- leetcode笔记--Compare Version Numbers