mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -209,8 +209,13 @@ Python:
|
|||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
|
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
|
||||||
|
# 先按照h维度的身高顺序从高到低排序。确定第一个维度
|
||||||
|
# lambda返回的是一个元组:当-x[0](维度h)相同时,再根据x[1](维度k)从小到大排序
|
||||||
people.sort(key=lambda x: (-x[0], x[1]))
|
people.sort(key=lambda x: (-x[0], x[1]))
|
||||||
que = []
|
que = []
|
||||||
|
|
||||||
|
# 根据每个元素的第二个维度k,贪心算法,进行插入
|
||||||
|
# people已经排序过了:同一高度时k值小的排前面。
|
||||||
for p in people:
|
for p in people:
|
||||||
que.insert(p[1], p)
|
que.insert(p[1], p)
|
||||||
return que
|
return que
|
||||||
|
Reference in New Issue
Block a user