LeetCode #406 - Queue Reconstruction by Height

AD在建PCB封装时如何添加元器件的3D模型 3D模型 阅读详情

题目描述:

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]]

一组数组表示一队人,h表示高度,k表示他之前的最大可以挡住他(高度大于或等于h)的人数,求这个数组的排列方法,满足所有的人。

首先需要对数组排序,然后直接将元素(h,k)插入结果数组的第k位上,同时要保证后面插入的元素不会影响之前的元素,所以应该是h越高的元素先插入,因为h较小的元素插入不会挡住之前h较高的元素,同时当h相等时,应该是k越小的元素先插入,因为后面插入的k更大,即插入在之前的元素后面,保证不会挡住之前的元素。排序的方法直接重定义sort函数即可。

class Solution {
public:
    static bool comp(pair<int,int> a,pair<int,int> b)
    {
        if(a.first!=b.first) return a.first<b.first;
        else if(a.first==b.first) return a.second>b.second;
    }
    
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        vector<pair<int,int>> result;
        sort(people.begin(),people.end(),comp);
        for(int i=people.size()-1;i>=0;i--)
        {
            result.insert(result.begin()+people[i].second,people[i]);
        }
        return result;
    }
};


【SpringBoot】1、创建第一个SpringBoot项目 创建SpringBoot项目可以通过两种方式,1、通过访问:https://start.spring.io/,SpringBoot的官方网站进行创建SpringBoot项目;2、通过工具(例如:Idea)创建SpringBoot项目。本次使用开发工具:Idea创建我的第一个SpringBoot项目。 首先,打开我们的Idea开发工具 选择Create New Poject(创建一个新的项目) ... 阅读详情

相关推荐

LMS Virtual.Lab二次开发:声学仿真结果导出方法(VBScript、Python)

Siemens LMS Virtual.Lab 提供一套集成软件平台用以模拟机械系统的品质属性,如结构完整性、振动噪声、耐久性、系统动力学特性、驾驶的平顺性及操纵的稳定性等。

爱看书的小沐 6043

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

戴尔G3/AWCC用户必看:揪出WMI Provider Host疯狂占用的元凶,附5.4.35旧版回退保姆级教程

本文针对戴尔G3/AWCC用户遇到的WMI Provider Host异常占用问题,提供了详细的诊断方法和解决方案。通过分析AWCC 5.5.14.0版本的资源占用问题,指导用户回退到稳定版AWCC 5.4.35,并附上保姆级教程和预防措施,帮助用户彻底解决系统卡顿问题。

weixin_30892037的博客 570

sort学习 - LeetCode #406 Queue Reconstruction by Height

用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp;而使用key和reverse就快得多,因为只需要在排序开始时处理一次即可,因此在排序的时候能不用cmp就尽量不用 另外可以用operator函数中的itemgetter,和attrgetter实现基于key的(多级)...

weixin_30318645的博客 127

MPLAB修改工程名称方法

修改工程名称方法 如下图所示,工程名称为:Loar_V1.0_20210205 现将该工程名称修改为:Loar_V1.1_20210205 修改方法: ①找到工程文件路径,将工程名称修改为Loar_V1.1_20210205 注意:此时打开变成进行编译,会报错:Loar_V1.1_20210205.production.hex does not exist or is not an executable, ②找到工程下的 nbproject 文件夹 ③将nbproject文件夹下的文件全部打开,并且将

qq_26195179的博客 1774

从d、q轴电流到电机性能:FOC控制中角度偏移的实战影响与调试策略

本文深入探讨了永磁同步电机FOC控制中d、q轴电流与角度偏移对电机性能的影响及调试策略。通过分析角度偏移导致的效率下降、电流异常等问题,提供了实用的四步调试法和参数优化技巧,帮助工程师有效提升电机控制精度和效率。

weixin_29264201的博客 304

LeetCode:406. Queue Reconstruction by Height

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

Wisimer 208

从零开始安装使用opengauss数据库(高斯数据库)

openGauss(高斯数据库)的环境配置、安装方法、基本使用方法

qq_43764807的博客 1万+

【leetcode-Python】-贪心-406. Queue Reconstruction by Height

题目链接 https://leetcode.com/problems/queue-reconstruction-by-height/ 题目描述 假设一群人打乱顺序站成一个队列,数组 people 表示队列中人的一些属性(不一定按顺序)。people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面正好有 ki 个身高大于或等于 hi 的人。 请你构造并返回输入数组people 所表示的队列。返回的队列表示为数组 queue ,其中 queue[j] = [hj, kj] 是队列..

gulaixiangjuejue的博客 205

硅谷刷屏的ClawdBot,让Mac mini卖爆了!创始人爆料:一人开发、100% AI 写代码

这两天,个人 AI 助手 ClawdBot 席卷硅谷,国内外社交平台上全是关于它的讨论。 在试用后,网友们纷纷给出了很高的评价。“它是迄今为止最伟大的 AI 应用,相当于你 24 小时全天候专属 AI 员工。”Creator Buddy 创始人兼 CEO Alex Finn 盛赞道,“这就是他们 (Anthropic) 希望 Claude Cowork 呈现的样子。”

技术引领业务创新 1600

【Mode】多模干涉仪的设计 Multi-mode interferometer(MMI)

与 Splitter 器件一样,MMI 器件也是一种常见的分光器件,可以根据用户的需求将光束分成不同的比例。本文,我们将介绍如何仿真 MMI 器件。这里我们使用的是 1x2 MMI,其本质上是由四个部分组成的,中间的矩形多模式干涉区域以及左右两侧的三个 taper 模式转换区域。之前的教程中我们也提及了,为了获取稳定的模式场分布,通常,我们需要进行模式收敛性扫描。这里以官网给出的案例为例。如何提取 S 参数我们先暂时不做介绍,后续有时间了我们再做介绍。

u011699626的博客 1239

【leetcode】【M】406. Queue Reconstruction by Height【95】

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

sscssz的博客 442

ESP32 DW1000 UWB 室内定位系统

详细介绍ESP32 DW1000 UWB 室内定位系统的设计演示demo。

DT程序员的博客 1671

LeetCode 406.Queue Reconstruction by Height

题目 假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。 注意: 总人数少于1100人。 示例 输入: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] 输出: [[5,0], [7,0], [5,2], [6,1], [4,4], ...

Infi_zc 168

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

OV9734 DATASHEET,完整规格书

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

LeetCode | 0406. Queue Reconstruction by Height根据身高重建队列【Python】

LeetCode 0406. Queue Reconstruction by Height根据身高重建队列【Medium】【Python】【贪心】 Problem LeetCode Suppose you have a random list of people standing in a queue. Each person is described by a pair of integer...

Wonz 548

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

[Leetcode] 406. Queue Reconstruction by Height 直觉解释

406. Queue Reconstruction by Height https://leetcode.com/problems/queue-reconstruction-by-height/ 首先,我们找到最小的 height (h, k),这个数据对的最终位置应该是在 k + 1 的位置上。因为这个值是最小的值,所以其它的值就不小于它。 如果它不在 k + 1 这个位置上,比如在 k + 1...

CY_TEC的博客 292

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

leetcode 406. Queue Reconstruction by Height

题目406. Queue Reconstruction by Height等级:medium 标签:贪心思路现对people数组基于pair的第一个元素(身高)排序,身高从小到大,逐一将people[i]放到结果vector的合适位置,放置方法是,对于结果vector,从左往右跳过people[i].second个有效位置(即还未放置pair的位置,或该位置已经放置了一个pair, 但是pair.

alxandral_brother的博客 356

LeetCode(406):根据身高重建队列 Queue Reconstruction by Height(Java)

2019.10.20 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 先将数组按身高降序+人数升序排序,然后依次遍历并执行插入,将每一个人[h,k]插入到第k个位置即可。 例如对于示例数组的算法流程为: 排序后:[7,0], [7,1], [6,1], [5,0], [5...

NJU_ChopinXBP的博客 389

【LeetCode】406. Queue Reconstruction by Height (with Python code)

Leetcode地址:https://leetcode.com/problems/queue-reconstruction-by-height/#/description 题目描述 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

sdf57的博客 206

LeetCode 406. Queue Reconstruction by Height

LeetCode 406 首先想到的一个方法是,先根据他要在位置排序,然后每次插入的时候根据height来进行插入,把所有比自己矮的都略过(这样不会破坏当前矮个的位置要求),然后计算比自己高的个数,找到合适的位置,然后再过一遍把自己矮的都略过,进行插入。这样保证自己放在符合自己条件但是又是在最后的位置。 def reconstructQueueSlow(self, people: List[List[int]]) -> List[List[int]]: people = sor

frankguodongchen的博客 143

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

caisense的专栏 431

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), wherehis the height of the person andkis the number of people in front o...

路漫漫远修兮 168

【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

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

【LeetCode】406. Queue Reconstruction by Height

问题描述问题链接:https://leetcode.com/problems/queue-reconstruction-by-height/#/descriptionSuppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), w

墨染百城 436

Leetcode406.queue reconstruction by height

def reconstructQueue(people): people.sort(key=lambda x: (-x[0], x[1])) res = [] for h, k in people: res.insert(k, [h, k]) return res l = [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]] l.sort(key=lambda x: (-x[0], x[1])) # 二维数组.

技术奇的默默积累 198
上一篇: LeetCode #394 - Decode String
下一篇: LeetCode #416 - Partition Equal Subset Sum
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址