leetcode406 重建身高队列

题目描述:假设有打乱顺序的⼀群⼈站成⼀个队列,数组 people 表⽰队列中⼀些⼈的属性(不⼀定按顺序)。每个 people[i] = [hi, ki] 表⽰第 i 个⼈的⾝⾼为 hi ,前⾯ 正好 有 ki 个⾝⾼⼤于或等于 hi 的⼈。请你重新构造并返回输⼊数组 people 所表⽰的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个⼈的属性(queue[0] 是排在队列前⾯的⼈)
题目链接
思路描述: 身高矮的人,入队不会对身高高的人的排名造成影响,所以我们可以先让身高最高的人入队,其次让身高次高的人来插入

// input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
//subarray after step 1: [[7,0], [7,1]]
//subarray after step 2: [[7,0], [6,1], [7,1]]
//subarray after step 3: [[5,0], [7,0],[5,2], [6,1], [7,1]]
// subarray after step 4: [[5,0], [7,0],[5,2],[4,4], [6,1], [7,1]]
    public static int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] != o2[0] ? -o1[0] + o2[0] : o1[1] - o2[1];
            }
        });
        List<int[]> res = new ArrayList<>();
        for (int[] cur : people) {
            res.add(cur[1], cur);
        }
        return res.toArray(new int[people.length][]);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值