406. 根据身高重建队列
为保证权益,题目请参考 406. 根据身高重建队列(From LeetCode).
解决方案1
Python
python
from typing import List
import functools
def cmps(a,b):
if a[0] > b[0]:
return -1
elif a[0] == b[0]:
if a[1] < b[1]:
return -1
else:
return 1
else:
return 1
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=functools.cmp_to_key(cmps))
# print(people)
for i in range(len(people)):
num = 0
if num == people[i][1]:
# 移除
t = people.pop(i)
people.insert(0, t)
continue
for j in range(0, i):
if people[j][0] >= people[i][0]:
num += 1
if num == people[i][1]:
# 移除
t = people.pop(i)
people.insert(j+1, t)
break
return people
s = Solution()
print(s.reconstructQueue([[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38