题目如下:

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解题思路的更多相关文章

  1. 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

  3. LN : leetcode 406 Queue Reconstruction by Height

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

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

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

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

  6. Queue Reconstruction by Height

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

  7. 57.Queue Reconstruction by Height(按身高重建对列)

    Level:   Medium 题目描述: Suppose you have a random list of people standing in a queue. Each person is d ...

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

  9. 406. Queue Reconstruction by Height

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

随机推荐

  1. 从壹开始 [Admin] 之四 || NetCore + SignalR 实现日志消息推送

    缘起 哈喽大家周一好呀,感觉好久没有写文章了,上周出差了一次,感觉还是比坐办公室好的多,平时在读一本书<时生>,感兴趣的可以看看

  2. 【.NETCore开源】开弓没有回头箭

    2019.2.11 开工大吉!经过了半个月的休假,今天回归岗位重新拾起工作,却发现熟悉的代码生疏了.年前的计划回忆不起来了,俗称"节后综合症". 忆半月圈子 过年放假的前几天有多篇 ...

  3. 学习 JavaScript (六)核心概念:函数

    基本知识 函数对于我们来说,不算陌生的东西.中学就已经有了函数的概念,比如: y = f(x) 输入一个数 x,能够得到与之对应的一个数 y.也就是说,f(x) 的有一个返回值,这是函数在数学上的定义 ...

  4. 《k8s-1.13版本源码分析》- Scheduler启动前逻辑

    本文原始地址(gitbook格式):https://farmer-hutao.github.io/k8s-source-code-analysis/core/scheduler/before-sche ...

  5. Spring WebFlux开门迎客,却来了一位特殊客人

    话说Spring WebFlux已经出现有一段时间了,但是知道他的人并不是很多.这让他很是闷闷不乐. 还有更惨的是,那些敢于吃螃蟹的人在尝试了他之后,有的竟把代码重新改回到Spring MVC的同步模 ...

  6. SAP MM Storage Location Missing in MD04 Result?

    SAP MM Storage Location Missing in MD04 Result? Today I received a ticket from business team, a user ...

  7. Dotspatial 要素重叠部分去除

    private void toolStripButton32_Click(object sender, EventArgs e) { /重叠部分去除操作——测试成功 if (mapMain.Layer ...

  8. netdom remove 错误:netdom remove

    自己用错了命令,直接将加入域的计算机使用dsrm删除了,本来应该使用netdom remove的,结果在域控制器上使用netdom remove错误,在客户端上登录时一样提示:netdom remov ...

  9. zookeeper3.4.13集群安装

    环境: Centos7.6 Zookeeper3.4.13 Java1.8 安装前准备 安装java 官网下载jdk-8u201-linux-x64.tar.gz 备用 三台主机:192.168.2. ...

  10. Luogu P5285 [十二省联考2019]骗分过样例

    Preface ZJOI一轮被麻将劝退的老年选手看到这题就两眼放光,省选也有乱搞题? 然后狂肝了3~4天终于打完了,期间还补了一堆姿势 由于我压缩技术比较菜,所以用的都是非打表算法,所以一共写了5K- ...