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.

我的提交:

//17.3.30
//贪心
//lambda表达式
import java.util.Arrays;
import java.util.ArrayList;
public class Solution {
    public int[][] reconstructQueue(int[][] people) {
        //高度相等,则按照人数升序排列;高度不相等,按照高度降序排列
        Arrays.sort(people,(p1,p2)->p1[0]==p2[0]?p1[1]-p2[1]:p2[0]-p1[0]);
        ArrayList<int[]> list = new ArrayList<>();
        //排队
        for(int[] p:people){
            list.add(p[1],p);
        }
        //将list转化为数组
        return list.toArray(people);
        
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,967评论 0 23
  • ------告诉你,我是最容易灰心的了,一点小事情会使我三个月什么也不写,只在心里反复说“你是个普通人,傻瓜”! ...
    红发罗恩阅读 221评论 0 2
  • 终于我体会到一种不可表达的感觉 就像身处险境而不得大声喊叫 只能放心里 我无数次想过如果真的生病了会怎么办 答案我...
    Z021阅读 128评论 0 0
  • 暮 春 雪 粉拙晕远山,泪巧点朱唇。 凝霜催草色,沉雾促花颜。 梅散芳菲尽,曲终亦断肠。 东山升冷月,杯酒饮残阳。
    影子写手M阅读 584评论 2 5
  • 与同学做了个测试,发现自己是个不幸福的人。但是,我一直想要的是幸福,我也想每天过得充实而快乐,所以从现在开始,我要...
    落英繁华阅读 263评论 0 0