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 list = new ArrayList<>();
        //排队
        for(int[] p:people){
            list.add(p[1],p);
        }
        //将list转化为数组
        return list.toArray(people);
        
    }
}

你可能感兴趣的:(406. Queue Reconstruction by Height)