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 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 ,. Example Input:
[[,], [,], [,], [,], [,], [,]] Output:
[[,], [,], [,], [,], [,], [,]]
简单的理解就是给定一个二维数组P,二维数组中的每个元素n, n[0] 表示高度,n[1] 表示位置,即在n前面有n[1]个元素,他们的高度都大于或等于n[0]
解题的思路:
一、不借鉴任何Java自带的容器:
1.遍历二维数组P,选择数组中的一个元素,其x[0]是所有元素中最大的,如果存在两个以上,[0]值是最大且相等,那么就根据[1]的值,选择[1]最小的那个元素
2.利用数组记录选中的元素在数组P中的下标,防止下一轮循环的时候又选到它,同时自己在实现的时候还加上了一个判断标志
3.使用插入排序算法,将选中的元素x,根据其 x[1] 的值作为在新数组中的下标,插入到新的数组中。(这里我的实现是重新生成一个临时数组TMP,大小是上一轮的“新”数组大小+1),优先插入选中的元素,
4.重复以上步骤,直到结束
以下是代码:
public int[][] reconstructQueue(int[][] people) {
int[][] temp = new int[0][];
int[] label = new int[people.length];
//用于跳过已选择的元素
boolean next = true;
while (temp.length < people.length) {
int[] t = {0,0};
for (int i = 0; i < people.length; i++) {
for (int j=0;j<temp.length;j++){
if(label[j] == i){
next=false;
break;
}
}
if (people[i][0] > t[0] && next) {
t[0] = people[i][0];
t[1] = people[i][1];
label[temp.length] = i;
}else if(people[i][0] == t[0] && next && t[1]>people[i][1] ){
t[0] = people[i][0];
t[1] = people[i][1];
label[temp.length] = i;
}
next = true;
}
temp = this.sortPeople(temp, t);
}
return temp;
} private int[][] sortPeople(int[][] temp,int[] t){
int[][] temp2 = new int[temp.length+1][2];
if(temp.length == 0){
temp2[0][0] = t[0];
temp2[0][1] = t[1];
return temp2;
}
int label = 0 ;
for (int i=0;i<temp2.length;i++){
if(i == t[1]){
temp2[i][0]=t[0];
temp2[i][1]=t[1];
}else {
temp2[i][0] = temp[label][0];
temp2[i][1] = temp[label][1];
label ++;
} }
return temp2;
}
以上是自己看到这道题目后的思路,现在看来其实只是做了一件事:将原来乱序的二维数组排好序,以高度【0】为第一排序规则做降序,以数量【1】为第二排序规则做升序
下面是提交后看的人家最优的答案,里面使用了优先级队列以及LinkedList,其中,优先级队列用于排序,LinkedList用于定位
优先级队列要重写compartor方法,根据之前说的顺序进行排序
代码很容易理解,只要想到了就一定会做,所以自己查的还是一种思维方式,还是需要继续认真的学习。。。
public int[][] bestReslveMethod(int[][] people){
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
//实现从小到大排序,方便确定位置时优先安排高度小的
return a[0] != b[0]? Integer.compare(b[0],a[0]):Integer.compare(a[1],b[1]);
}
});
LinkedList<int[]> linkedList = new LinkedList<>();
for (int[] one : people){
priorityQueue.offer(one);
}
while (!priorityQueue.isEmpty()){
int[] one = priorityQueue.poll();
//此处根据one[1]确定该元素的位置
linkedList.add(one[1],one);
}
return linkedList.toArray(new int[people.length][2]);
}
LeetCode_406. Queue Reconstruction by Height解题思路的更多相关文章
- 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- 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) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
- 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 ...
- Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- 57.Queue Reconstruction by Height(按身高重建对列)
Level: Medium 题目描述: Suppose you have a random list of people standing in a queue. Each person is d ...
- [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 ...
- 406. Queue Reconstruction by Height
一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...
随机推荐
- 基于 Webpack 4 和 React hooks 搭建项目
面对日新月异的前端,我表示快学不动了
- 聊聊真实的 Android TV 开发技术栈
智能电视越来越普及了,华为说四月发布智能电视跳票了,一加也说今后要布局智能电视,在智能电视方向,小米已经算是先驱了.但是还有不少开发把智能电视简单的理解成手机屏幕的放大,其实这两者并不一样. 一.序 ...
- 5.3Role和Claims授权「深入浅出ASP.NET Core系列」
希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,码字辛苦,如果你吃了蛋觉得味道不错,希望点个赞,谢谢关注. Role授权 这是一种Asp.Net常用的传统的授权方法,当我们在 ...
- Mybaits-plus实战(二)
1. Mybaits-plus实战(二) 1.1. mybatis-plus插件 1.1.1. 用法 先举个例子介绍用法,如下:直接作为Bean注入,一般来讲插件太多印象性能,所以大部分插件都只在测试 ...
- sqlserver的坑
1.今天系统出现BUG,经过两个小时的排查,发现是存储过程中的SELECT @@IDENTITY的值发生错乱,导致的系统BUG,经过百度,发现这个函数貌似和触发器有冲突,一旦插入的表有触发器的话,@@ ...
- IdentityServer4 知多少
1. 引言 现在的应用开发层出不穷,基于浏览器的网页应用,基于微信的公众号.小程序,基于IOS.Android的App,基于Windows系统的桌面应用和UWP应用等等,这么多种类的应用,就给应用的开 ...
- 4. VIM 系列 - 认识VIM的缓冲区、窗口、标签页
目录 1. 缓冲区 2. 窗口 3. 标签页 4. 设置一下热键 1. 缓冲区 文件和缓冲区的区别 vim 打开一个文件时,其实是从磁盘中读取文件到内存中,vim的一些操作其实是在操作缓冲区, 当使用 ...
- AI - 深度学习之美十四章-概念摘要(1~7)
原文链接:https://yq.aliyun.com/topic/111 本文是对原文内容中部分概念的摘取记录,可能有轻微改动,但不影响原文表达. 01 - 一入侯门"深"似海,深 ...
- Java相关面试题总结+答案(三)
[多线程] 35. 并行和并发有什么区别? 并行:多个处理器或多核处理器同时处理多个任务.(是真正的物理上的同时发生) 并发:多个任务在同一个 CPU 核上,按细分的时间片轮流(交替)执行,从逻辑上来 ...
- body标签中l的相关标签
字体标签: h1~h6.font. u.b,.strong. em. sup. sub 排版标签: div,.span.br.hr.center.pre 图片标签: img 超链接: a 列表标签: ...