leetcode 406. Queue Reconstruction by Height (greedy)

ARTIX-7 GTP 3G-SDI 工程构建与调试实战 本文详细介绍了基于ARTIX-7 FPGA和GTP收发器的3G-SDI工程构建与调试实战。从基础概念解析到Vivado工程创建、GTP IP核配置、SDI IP核集成,再到系统集成与时钟域处理,提供了全面的技术指导。文章还分享了调试技巧、常见问题排查方法以及性能验证步骤,帮助开发者快速掌握3G-SDI视频传输技术的实现与优化。 阅读详情

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.

Note:
The number of people is less than 1,100.

Example

Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

数组中的两个数一组用pair表示
题目中的pair[0]表示身高,pair[1]表示这个人前面还有几个人比他高,要求输出的排序是
看pair[1], 比如[4,4],pair[1]=4表示前面有4个人比他高,那么它前面必须得有4个元素的pair[0]>=4

思路:
greedy

就说题中的input,[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
遇到[4,4]的时候知道前面还有4个元素要>=pair[0]=4,但是从左到右遍历的时候只看到一个7比4大,这时候就需要在后面再找出3个比4大的元素,同时还要照顾到pair[1]
这时候需要同时考虑两个因素,一个身高,一个是位置

这时候应该降维,固定一个因素,只考虑剩下的一个因素
固定位置显然不行,因为位置不是绝对位置,它是由前面人的身高决定的,所以要固定身高这个因素

题中说了前面有几个人比自己“高”,所以高的排在前面,按从高到低排序,这样有一个好处,
因为是从高到低
7, 7, 6, 5, 5, 4
如果要求6前面有1个人比它高,4前面有两个人比它高
从左到右遍历的时候把6直接移到第二位
7, 6, 7, 5, 5, 4
4前面有两个人比它高:
因为排好序的结果是4前面所有元素都比4大,所以前面的移动不影响比4大的元素个数,同时4不管移动到前面哪个位置,都能保证那个位置之前的元素是比4大的
所以要求4前面有2个人比它高,那么可以直接把4移到第3个位置上,而不需要考虑前面2个是不是会比4小

pair[1]相同的情形:
比如[5, 0], [7, 0]
还是按身高大的排前面,因为更便于后面的元素不需要考虑身高而直接按位置移动

pair[0]相同的情形:
[7,0], [7, 1]
pair[0]小的排在前面,因为不需要再额外移动了

**注意java的Comparator的compare方法,参数一定要是Object的,用的时候需要down cast
还有就是asList()和toArray()

    public int[][] reconstructQueue(int[][] people) {
        if (people == null || people.length <= 1) {
            return people;
        }
        
        Arrays.sort(people, new Comparator(){
           public int compare(Object p1, Object p2) {
               if (((int[])p1)[0] == ((int[])p2)[0]) {
                   return (((int[])p1)[1] - ((int[])p2)[1]);
               } else {
                   return (((int[])p2)[0] - ((int[])p1)[0]);
               }
           } 
        });
        
        ArrayList<int[]> list = new ArrayList<>();
        
        for (int[] p : people) {
            list.add(p[1], p);
        }
        
        return list.toArray(people);
    }
详解电路设计中的RC电路 ​ 一、什么是RC电路 RC电路是指由电阻R和电容C组成的电路,它是脉冲产生和整形电路中常用到 的电路。 常见的RC充、放电电路、RC积分电路合RC微分电路接下来详细讲解。 二、RC充、放电电路 RC充、放电电路如下图所示,下面通过充电和放电两个过程来分析此电路。 ​RC充、放电电路图 (1)RC充电电路 RC充电电路图如下图(a)所示 RC充电电路图及充电曲线图 ​由上图分析: 将上图a中开关S置于“1”处,电源E开始通过电阻R对电容C充电,由于刚开始充电时电容两端没有.. 阅读详情

相关推荐

车载以太网SOMEIP入门必看:从服务发现到事件订阅,一次搞懂三种核心通信模式(附报文实例)

本文深入解析车载以太网SOME/IP的三种核心通信模式:服务发现、方法调用和事件订阅,通过真实报文实例和仿真工具演示,帮助开发者快速掌握这一关键技术。文章特别强调服务发现机制的基础作用,并详细介绍了Request/Response与Fire&Forget两种方法调用模式的应用场景,为智能汽车网络通信开发提供实用指南。

weixin_30955617的博客 120

LeetCode 406. Queue Reconstruction by Height 解题报告

LeetCode 406. Queue Reconstruction by Height 解题报告

骆小坑的填坑之旅 5432

手推反向传播+numpy实现

反向传播的重要性不必多说,手推也是必备基础,大厂面试要求用numpy实现一下BP也是经常的事。下面以一个简单的两层网络为例(简单到连bias都没有的那种),真·手推反向传播+numpy实现。 说明: input:输入 net_h:隐层 S(x):sigmoid简写 SD(x):sigmoid导数的简写 out_h:隐层经过sigmoid激活的输出 net_o:输出层 out_o:经过经过sigmoid激活的输出层,也就是最后的输出 target(简写t1、t2):Ground...

xian0710830114的专栏 1241

leetcode——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 t

duchy_8019的博客 647

Microchip Studio 7.0 从零开始:下载、安装到首个AVR工程实战

本文为AVR单片机开发新手提供了Microchip Studio 7.0的完整入门指南。从官方下载、避坑安装,到创建首个GCC C工程、编写LED闪烁代码,再到项目配置、仿真调试与程序烧录,手把手带你完成从零到一的实战过程,是掌握AVR开发环境的必备教程。

weixin_29200741的博客 549

剑灵解包器修改xml.dat文件修改乳量方法

本篇内容是基于单机版网游改单机版游戏剑灵的内容,主要针对关于单机版进行数据修改,当然了对于网端有关于剑灵已有很多,像小助手等多种可以更改,但是存在有封号危险,好,下面我们是在单机版中来进行乳量的修改教程。

灵魂学者的博客 3410

算法(九)贪心算法之排队问题

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 t

zhougb3的博客 5394

硬全桥副边钳位管Vds电压凸台揭秘——寄生参数引发的导通异常

本文深入分析了硬全桥副边钳位管Vds电压凸台现象,揭示了寄生参数如何引发导通异常。通过波形分析和工程实践,详细解析了走线电感的蓄能效应和体二极管的接力作用,并提供了PCB布局优化、元件选型和驱动电路调整等解决方案,帮助工程师有效应对这一常见问题。

weixin_30825581的博客 439

python--leetcode406. 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 t

wenqiwenqi123的博客 1291

最新Codex接入DeepSeek官方API教程(无需cc-switch)

最新Codex接入DeepSeek官方API教程(无需cc-switch)

猿小白的博客 1051

【目录】专栏文章导航

从最强工具链Davinci Configurator模块学习出发掌握AUTOSAR

weixin_47571898的博客 3331

LeetCode_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 ...

212

Python反爬实战|动态渲染+高可用IP代理集群,稳定爬取京东/淘宝10万+商品数据

做Python电商爬虫的同学,应该都对京东、淘宝的反爬机制深有体会——这两个平台的反爬体系,堪称国内网站的天花板,远比普通资讯、政务网站的反爬严格十倍不止。用requests库请求商品列表,返回的永远是只有骨架的空HTML,商品标题、价格、销量全是前端JS异步加载,抓包分析接口发现参数全是加密的signtoken,解密成本无限高;用selenium开无头浏览器爬取,刚爬3-5页就触发风控,页面直接返回403,IP被封禁,甚至连Cookie都直接失效;

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1595

leetcode 406. Queue Reconstruction by Height

leetcode 406. Queue Reconstruction by HeightProblemSolutionGreedy Problem 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 i...

weixin_45764148的博客 182

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LeetCode #406: Queue Reconstruction by Height

Problem Statement(Source) 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

浮生琴弦 618

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Greedy(medium三道题)

55. Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. De...

xw_Summer的博客 395

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

第35天 Greedy 860、406、452

第35天 Greedy 860、406、452

Kathrynzzz的博客 115

Jasna 免费视频马赛克修复指南:RTX 5090 上 34 秒修完 1080p

你有一段 1080p 视频,画面里盖着马赛克,想把它修干净。用 Jasna,RTX 5090 上整段 3 分 22 秒的 1080p 视频,免费 BasicVSR++ 模型修完只要 **34 秒**。它是个 GPU 视频马赛克修复工具:检测、跟踪、修复、拼接、编码全自动,图形界面和命令行都能用。官方发行包按系统和 GPU 厂商下载解压即可,不用自己装依赖。 ## 先判断:你的设备适不适合跑 |

gitblog_00453的博客 438

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 p

lmy690858904的专栏 766

题解——Leetcode 406. Queue Reconstruction by Height 难度:Medium

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

dxk3881345的博客 562

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 t

simple008的博客 631

Leetcode解题报告: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 fro

wengyunpeng的博客 513

【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)

【LeetCode】Queue Reconstruction by Height 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/queue-reconstruction-by-height/#/description题目描述:Suppose you have a random list of people standing in a

负雪明烛 4515

[LeetCode 6.6] Queue Reconstruction by Height

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

xvrixingkong的博客 155

[Leetcode] Queue Reconstruction by Height 解题思路

Problem: 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

misterbobo的博客 452

【信息奥赛】CSP-J 二维区间和(前缀和的应用)、二维差分

本文介绍了二维前缀和与二维差分的应用。通过构建二维前缀和数组,可在O(1)时间内快速求解任意子矩阵的和;而二维差分则支持对矩形区域的批量加减操作,结合前缀和还原原数组。两者均基于前缀和思想,是高效处理二维区间修改与查询问题的核心技巧。

Anne_Stewart的博客 219
上一篇: leetcode 392. Is Subsequence
下一篇: leetcode 435. Non-overlapping Intervals
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址