栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

6.<tag-贪心算法 + 双维度贪心>lt.406. 根据身高重建队列

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

6.<tag-贪心算法 + 双维度贪心>lt.406. 根据身高重建队列

lt.406. 根据身高重建队列 [案例需求]

[思路分析]

[代码实现]

class Solution {
    public int[][] reconstructQueue(int[][] people) {
        //1. 先按身高排队(从大到小), 并且对于同一身高的, k小的放前面
        Arrays.sort(people, new Comparator(){
            public int compare(int[] arr1, int[] arr2){
                if(arr1[0] == arr2[0])return arr1[1] - arr2[1];
                return arr2[0] - arr1[0];
            }
        });
        //2. 我们从身高最高处(左边), 按照k的值依次插入到合适的位置
        int rowLen = people.length;
        int colLen = people[0].length;

        LinkedList queue = new LinkedList<>();

        for(int[] p : people){
            queue.add(p[1], p);
        }
        // for(int i = 0; i < rowLen; i++){
        //     int targetIndex = people[i][1];
        //     System.out.println(targetIndex);
        //     if(targetIndex == i)continue;

        //     //两个数组相互交换位置
        //     int[] temp = people[i];
        //     people[i] = people[targetIndex];
        //     people[targetIndex] = temp;
        // }

        //return people;
        return queue.toArray(new int[people.length][]);
    }
}
lt.240-搜索二维矩阵 || [案例需求] [思路分析] [代码实现]
 
lt.240-搜索二维矩阵 || 
[案例需求] 
[思路分析] 
[代码实现] 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976370.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号