Skip to content

Commit 0510c6d

Browse files
committed
406. 根据身高重建队列
1 parent a2ad2b7 commit 0510c6d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

greed/406_reconstructQueue.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
6+
people.sort(key=lambda x: (x[0], -x[1]))
7+
n = len(people)
8+
ans = [[] for _ in range(n)]
9+
for person in people:
10+
spaces = person[1] + 1
11+
for i in range(n):
12+
if not ans[i]:
13+
spaces -= 1
14+
if spaces == 0:
15+
ans[i] = person
16+
break
17+
return ans
18+
19+
def reconstructQueue_2(self, people: List[List[int]]) -> List[List[int]]:
20+
people.sort(key=lambda x: (-x[0], x[1]))
21+
n = len(people)
22+
ans = list()
23+
for person in people:
24+
ans[person[1]:person[1]] = [person]
25+
return ans
26+
27+
28+
if __name__ == '__main__':
29+
people = [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]]
30+
solution = Solution()
31+
print(solution.reconstructQueue_2(people))

0 commit comments

Comments
 (0)