LeetCode OJ 406. Queue Reconstruction by Height

智驾系统分层架构:从传感器到执行器的全链路解析 本文深入解析了智能驾驶系统的分层架构,从传感器数据采集融合、应用层决策到车辆管理层执行,完整呈现了从感知到控制的全链路工作流程。文章重点阐述了架构设计的核心优势在于接口标准化与模块解耦,以ACC和LKA协同场景为例,说明了各层级如何通过清晰分工与高效协作,实现复杂系统的可靠、灵活与易于升级。 阅读详情

LeetCode OJ 406. 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 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大的元素有k个。题意是要把这样的元素组成的队列排序。首先把元素排序:按照h的降序排列,h相等时,按照k的升序排列。然后我们再一个个把元素插入到结果队列中。插入的方法是:插入到当前队列下标为k的位置。因为我们之前插入的都是比这个元素的h值大的元素,因此当前元素的h值就是队列中所剩下未插入结果队列的h值最大的元素了。而且k值是升序排列的,保证了结果插入的过程中不会越界。
eg.
题目中所给的输入进行排序后
Input: [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]
结果Result首先插入第一个元素[7,0]:
Result: [[7,0]]
插入第二个元素[7,1],在Result中下标为1的位置,即偏移量为1:
Result: [[7,0], [7,1]]
插入第三个元素[6,1]:
Result: [[7,0], [6,1], [7,1]]
插入第四个元素[5,0]:
Result: [[5,0], [7,0], [6,1], [7,1]]
插入第五个元素[5,2]:
Result: [[5,0], [7,0], [5,2], [6,1], [7,1]]
插入第六个元素[4,4]:
Result: [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

Note:

std::sort函数是全局的,如果第三个参数cmp不是静态成员函数或者全局函数,运行都会报错。非静态函数是依赖于具体对象的,因此调用sort函数时,只能调用不依赖于对象的静态函数cmp。

代码

个人github代码链接

class Solution {
public:
    static bool cmp(pair<int, int> p1, pair<int, int> p2){
        return (p1.first > p2.first) || ((p1.first == p2.first) && (p1.second < p2.second));
    }
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        vector<pair<int, int>> ans;
        sort(people.begin(), people.end(), cmp);
        for(auto p:people){
            ans.insert(ans.begin() + p.second, p);
        }
        return ans;
    }
};

国科大学习资料--高级软件工程-复习题选择题答案 国科大学习资料–高级软件工程(罗铁坚 魏峻 刘丹)-复习题选择题答案 ’‘ 阅读详情

相关推荐

注册电气工程师详细教材

注册电气工程师详细教材

qq_40844444的博客 4309

LeetCode oj 406. Queue Reconstruction by Height(优先队列)

406. Queue Reconstruction by Height   Question Editorial Solution  My Submissions Total Accepted: 1911Total Submissions: 3604Difficulty: Medium Suppose you have a random

Tristeza的博客 846

Case-Based Reasoning用于RAG

在做大模型应用RAG的时候,会发现一种特殊的场景,就是基于之前的case进行回答,比如法律咨询、医疗诊断等等。这种情况的问答,需要有强有力的case支持才行,有了case支持,大模型回答会更加准确。CBR这个概念已经很久了,它是一种人工智能推理方法,它的基本思想是:遇到新问题时,先回忆相似的过去案例,再基于这些案例进行调整,得到新问题的解答。Retrieve(检索):在案例库中找到与当前问题最相似的案例。Reuse(复用):借鉴案例的解决方案来应对当前问题。

sinat_28694519的博客 943

LeetCode OJ 451. Sort Characters By Frequency

LeetCode OJ 451. Sort Characters By FrequencyDescriptionGiven a string, sort it in decreasing order based on the frequency of characters.Example1 Input: “tree” Output: “eert” Explanat

jan_deng的博客 507

python模拟银行交易流水

模拟自动生成银行交易流水,其中包括:入账账号、出账账号、交易金额、交易余额、交易时间、出入表示关键字段

qq_31823889的博客 2923

Docker Desktop 在 Windows 上的安装和使用

WSL 指 “”,这是一个在 Windows 10\11 上能够运行原生 Linux 二进制可执行文件(ELF格式)的兼容层。它是由微软与Canonical公司合作开发的,旨在使纯正的Ubuntu、Debian等映像能下载和解压到用户的本地计算机,并且映像内的工具和实用工具能在此子系统上原生运行。这极大地拓展了Windows系统的功能,使用户能够在Windows环境中享受到Linux的强大功能。//Linux容器。

swadian2008的博客 6万+

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder TraversalDescriptionGiven inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do n

jan_deng的博客 488

VS2019 +osg3.6.5+ earth3.2+qt5.14.2+osgQt 编译教程

用vs新建工程,稍后会在写一篇博客上分享四、结语至此,所有的编译就均已完成。

king_ink的博客 1142

LeetCode OJ 12. Integer to Roman

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.解题思路整数转换为罗马数字,用一个二维数组保存1-9,10-90,100-900

jan_deng的博客 441

Docker新手必看:解决‘Unable to find image’错误的完整指南

本文针对Docker新手常见的‘Unable to find image’错误,提供了完整的排查与解决指南。文章深入剖析了权限不足、网络连接失败、Docker服务未运行等核心原因,并给出了将用户加入docker组、配置镜像加速器等实用解决方案,帮助读者彻底理解并解决镜像拉取问题。

weixin_29230901的博客 256

【零基础学习CAPL】——CAN报文的发送(CRC——ChecSum值)

本章主要介绍带有CRC值的报文发送一般报文可使用CANoe的IG模块直接发送,但带有CRC值的报文就只能编写脚本进行发送,以下详细讲解在配合Panel面板情况下,如何进行带有循环冗余码的报文进行报文的发送。

LOVE135149的博客 1950

LeetCode OJ 515. Find Largest Value in Each Tree Row

LeetCode OJ 515. Find Largest Value in Each Tree RowDescriptionYou need to find the largest value in each row of a binary tree.Example Input: 1 / \ 3 2 / \ \ 5 3 9 O

jan_deng的博客 432

双馈风机分段控制策略的一次调频模型深度解析

双馈风机分段调频控制策略研究 摘要:针对高比例风电并网导致的系统频率稳定问题,本研究提出一种基于风速分区的双馈风机一次调频控制策略。该策略根据风速区间(低、中、高)特性差异,分别采用:1)低风速区以转子动能控制为主;2)中速区结合超速备用与动能释放;3)高速区引入桨距角协调控制。通过构建包含空气动力学、轴系动态的完整数学模型,验证了分段控制在调频能力(最大提升40%功率支撑)、转速安全范围(控制在0.7-1.2pu)和动态响应速度(200ms内快速响应)等方面的优势。

huanghm88的专栏 350

LeetCode OJ 207. Course Schedule

LeetCode OJ 207. Course Schedule典型的拓扑排序算法。谨以此题纪念期末机考时没AC这么简单的拓扑排序。upset……ingDescriptionThere are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for

jan_deng的博客 373

OV9734 DATASHEET,完整规格书

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

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode OJ 240. Search a 2D Matrix II

LeetCode OJ 240. Search a 2D Matrix IIDescriptionWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in

jan_deng的博客 363

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LeetCode OJ 13. Roman to Integer

LeetCode OJ 13. Roman to IntegerDescriptionGiven a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.解题思路罗马数字转换为整数:我们把1,10,100,1000,5,500,500和对应的罗马数字做映射。

jan_deng的博客 356

老版QQ原生表情gif动态

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

LeetCode OJ 566. Reshape the Matrix

LeetCode OJ 566. Reshape the MatrixDescriptionIn MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’r

jan_deng的博客 332

LeetCode OJ 155. Min Stack

LeetCode OJ 155. Min StackDescriptionDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element o

jan_deng的博客 330

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder TraversalDescriptionGiven preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not

jan_deng的博客 324

LeetCode OJ 496. Next Greater Element I

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionYou are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nu

jan_deng的博客 313

LeetCode OJ 141. Linked List Cycle

LeetCode OJ 141. Linked List CycleDescriptionGiven a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?解题思路对于判断环的问题,可以采用双指针(快慢指针)的方法来判断。另外,因为题目中给的Lis

jan_deng的博客 308

LeetCode OJ 399. Evaluate Division

LeetCode OJ 399. Evaluate DivisionDescriptionEquations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some

jan_deng的博客 300

LeetCode OJ 226. Invert Binary Tree

LeetCode OJ 226. Invert Binary TreeDescriptionInvert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9to 4 / \ 7 2 / \ / \ 9 6 3 1方法一Recursion: 递归,每一次翻转左右子树。这种方法要每次

jan_deng的博客 300

LeetCode OJ 405. Convert a Number to Hexadecimal

LeetCode OJ 405. Convert a Number to HexadecimalDescriptionGiven an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.Note: All letters in

jan_deng的博客 294

LeetCode 热题 100-普通数组

因为nums[i]应该在的位置索引是nums[i]-1,所以如果nums[nums[i]-1](按索引找出来的值)不是nums[i],那就换位置。lambda是匿名函数,lambda表达式本身产生一个函数对象,可以赋值给变量,传给 函数,立即调用。找半天错误,发现不该和intervals前一个区间做对比,应该和list1的最后一个区间做对比。我原本的想法是创建一个新列表,把轮转后的数字放在新列表中,然后把新列表赋值给nums。列表原地排序,排序依据是每个元素(一个区间)的第 0 个值,即左端点。

m0_74153997的博客 167

LeetCode100 22.括号生成

该解法通过深度优先搜索生成所有合法括号组合。使用递归时传入剩余左括号数 left 和右括号数 right,剪枝条件为 left > right(防止右括号多于左括号),非法则返回。每次递归尝试添加左括号或右括号,当两者均为0时,得到一组合法结果。因字符串不可变,无需回溯,直接传递新串。最终返回所有合法括号组合。

Shaoxingzong的博客 162
上一篇: LeetCode OJ 56. Merge Intervals
下一篇: LeetCode OJ 513. Find Bottom Left Tree Value
jan_deng
博客等级 码龄11年 1粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值