python按身高体重排队_LeetCode | 0406. Queue Reconstruction by Height根据身高重建队列【Python】...

这是一个关于LeetCode第0406题的解决方案,题目要求根据人们的身高和前面比自己高的人数重建队列。可以使用贪心算法,先按身高降序、人数升序排序,然后依次将人插入到正确的位置。提供的Python代码实现了这一逻辑,通过排序和插入操作重建了队列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 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 的人数。 编写一个算法来重建这个队列。

注意:

总人数少于1100人。

示例

输入:

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

输出:

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

思路

贪心

首先按照身高 h 从高到低,k 从小到大排序。

然后只需插入即可,可以看代码注释。

这里拿示例来举例:

输入:

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

排序:

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

比如[6,1],表示前面比 6 高的只有一个,那么自然就插入到位置1(从0开始数):

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

以此类推

[5,0]:

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

[5,2]:

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

[4,4]:

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

end

时间复杂度: O(len(people))

空间复杂度: O(1)

Python代码

class Solution(object):

def reconstructQueue(self, people):

"""

:type people: List[List[int]]

:rtype: List[List[int]]

"""

people.sort(key = lambda x : (-x[0], x[1])) # 按照h从高到低,k从小到大排序

res = []

for p in people:

res.insert(p[1], p) # 每次只要在p[1]位置插入p就行,因为p[1]表示p前只能出现的个数

return res

代码地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值