LeetCode 406.根据身高重建队列 | 解题思路及代码

There are (n) people, we want them line up in the following way.

Given a two-dimensional array: (people[n][2]), where (people[i]=[h_i][k_i]), (h_i) means the height of (i)-th person, (k_i) means there are (k_i) people who are the same height as person (i) or taller than person (i) stand in front of person (i). So there are actually (k_i) or more people stand in front of person (i).

According to the two-dimentional array, line them up. It’s promised that them can be lined up.

people =([[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]])
answer =([[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]])

Firstly, let’s consider a simpler case: people’s height differs. No two people are the same height.

Assume there are a array (line[n]). We want to put everyone in this line. When we put (people[i]=[h_i][k_i]), assuming no one taller than him has been lined. We need to reserve (k_i) spaces in front of him for those (k_i) people who are higher than him. So we put person (i) at the (k_i+1)-th empty position.

How to ensure “no one taller than him has been lined”? That’s sample, just put them in the line in ascending order of height.

Now we consider how to handle people who have same height.

For person (i), (j), we assume their height are the same. Without loss of generality, we assume (k_i

Sort (people) array according to (h_i) as the first keyword in ascending order and (k_i) as the second keyword in descending order.

Put the sorted (people[i]) in the (k_i+1)-th position in turn.

The detailed code is as follows

public class p406 {
    public int[][] reconstructQueue(int[][] people) {
        int num = people.length;
        Arrays.sort(people, new Comparator() {
            public int compare(int[] t1, int[] t2) {
                return t1[0] == t2[0] ? t2[1] - t1[1] : t1[0] - t2[0];
            }
        });
        int[][] ans = new int[num][];
        for (int[] person : people) {
            int space = person[1] + 1;
            for (int i = 0; i < num; i++) {
                if (ans[i] == null) {
                    space--;
                }
                if (space == 0) {
                    ans[i] = person;
                    break;
                }
            }
        }
        return ans;
    }
}

Time complexity: (O(\log n)) for sort, (O(n^2)) for going through (people) array and find corresponding location for each person. So the total time complexity is (O(n^2)).

Space complexity: Sorting is implemented by recursive calls. So the time complexity is (O(\log n)).

Original: https://www.cnblogs.com/liuzhch1/p/16183496.html
Author: liuzhch1
Title: LeetCode 406.根据身高重建队列 | 解题思路及代码

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606684/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • zabbix自定义监控mysql主从状态和延迟

    zabbix自定义监控mysql主从状态和延迟 zabbix自定义监控mysql主从状态和延迟 zabbix自定义监控mysql主从状态 zabbix自定义监控mysql主从延迟 …

    Linux 2023年6月13日
    0117
  • 自己写的文件夹图标修改脚本

    自己写了一个文件图标修改的Python脚本,只要把文件夹拖动到这个脚本上,就可以用文件夹中的图片和视频作为文件夹的封面。把图片或视频拖到脚本上,就可以把这个图片或视频用作其所在文件…

    Linux 2023年6月6日
    0159
  • powershell配置自动补全

    powershell配置自动补全 一、需求: 看到老师上课用mac命令行有自动补全功能,发现真的爽。但是自己的windows powershell不能使用自动补全功能。有了需求,就…

    Linux 2023年6月13日
    0131
  • JVM学习 类加载子系统

    JVM 哔哩哔哩 尚硅谷视频 宋红康老师 Java代码执行流程 简图 详细图 1、类加载子系统 类加载器子系统的作用 类加载器子系统负责从文件系统或者网络中加载Class文件,cl…

    Linux 2023年6月7日
    0101
  • 微信公众号开发之获取微信用户的openID

    (注:openID同一用户同一应用唯一,UnionID同一用户不同应用唯一。不同应用指微信开放平台下的不同用户。) 1、 申请测试号(获得appID、appsecret) 2、 填…

    Linux 2023年6月13日
    077
  • 【论文笔记】(2015,JSMA)The Limitations of Deep Learning in Adversarial Settings

    本文是早期的对抗文章,发表于 EuroS&P 2016会议,最主要的工作是:提出了一个生成对抗样本的算法– JSMA(Jacobian Saliency Map…

    Linux 2023年6月7日
    090
  • UWP 自定义密码框控件

    1. 概述 微软官方有提供自己的密码控件,但是控件默认的行为是输入密码,会立即显示掩码,比如 *。如果像查看真实的文本,需要按查看按钮。 而我现在自定义的密码控件是先显示你输入的字…

    Linux 2023年6月13日
    089
  • Spring事务管理,声明式事务和编程式事务实现

    数据库操作过程中,对于增删改等操作,因为涉及到数据库状态的变更,为保证数据安全,需要进行事务管理;Spring事务管理有两种方式,即声明式事务管理和编程式事务管理; 连接池配置: …

    Linux 2023年6月16日
    0196
  • USB转双串口产品设计-TTL串口

    基于USB转2路串口芯片CH342,可以为各类主机扩展出2个独立的串口。CH342芯片支持使用操作系统内置的CDC串口驱动,也支持使用厂商提供的VCP串口驱动程序,可支持Windo…

    Linux 2023年6月7日
    0100
  • 实验一-密码引擎-加密API实现与测试

    任务详情 1 下载并查找GMT 0018-2012密码设备应用接口规范原始文档进行学习 (5分) 2 实现GMT 0018-2012密码设备应用接口规范的接口函数,至少实现:1)设…

    Linux 2023年6月8日
    082
  • 【Leetcode】62. 不同路径

    一个机器人位于一个 m x n网格的左上角 (起始点在下图中标记为 “Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在…

    Linux 2023年6月6日
    0111
  • 关于在Rocky linux下安装dotnet sdk不成功的问题

    Rocky Linux 9,运行 dnf install -y dotnet-sdk-6.0 一切正常,运行起来非常顺利,安装完毕。但是非常诡异,运行 dotnet –list-…

    Linux 2023年6月6日
    0116
  • SQLI-LABS(Less-4)

    Less-4(GET-Error based-Double Quotes-string) 打开 Less-4页面,可以看到页面中间有一句 Please input the ID a…

    Linux 2023年6月6日
    079
  • MySQL主从复制的原理和实现

    垂直扩展: 横向扩展: 复制:使每一个节点都有相同的数据集 MySQL复制的实现:使用二进制日志来实现 提高性能(负载均衡)、 实现读写分离 实现数据备份的功能(实时备份) 高可用…

    Linux 2023年6月7日
    0100
  • 《分布式系统原理介绍》读书笔记

    1、在大型集群中每日宕机发生的概率为千分之一左右;在实践中,一台宕机的机器恢复时间通常认为是 24 小时。 2、由于网络数据丢失的异常存在,直接决定了分布式系统的协议必须能处理网络…

    Linux 2023年6月16日
    0101
  • srec_cat 常用参数的使用

    下面介绍映像文件工具 srec_cat 的使用,如何通过相关参数实现自己需要的功能。 文件类型 在输入文件和输出文件文件时要指明文件类型,常用的如: test.hex -intel…

    Linux 2023年6月7日
    084
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球