-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path406_queue_reconstruction_by_height.py
More file actions
79 lines (56 loc) · 1.54 KB
/
406_queue_reconstruction_by_height.py
File metadata and controls
79 lines (56 loc) · 1.54 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Main Concept:
1. group the mans with same height and record origin index
2. record all heights
3. sort heights and iterate from end to start
4. get the corresponding mans and insert to `k` index
input: [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
heights: [4, 5, 6, 7]
process:
7 [(0, 0), (1, 2)] # input[0], input[2]
[[7, 0]]
[[7, 0], [7, 1]]
6 [(1, 4)]
[[7, 0], [6, 1], [7, 1]]
5 [(0, 3), (2, 5)]
[[5, 0], [7, 0], [6, 1], [7, 1]]
[[5, 0], [7, 0], [5, 2], [6, 1], [7, 1]]
4 [(4, 1)]
[[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
"""
class Solution:
def reconstructQueue(self, people):
"""
:type people: List[List[int]]
:rtype: List[List[int]]
"""
ans = []
if not people:
return ans
people.sort(key=lambda p: (p[0], -p[1]))
for i in range(len(people) - 1, -1, -1):
ans.insert(people[i][1], people[i])
return ans
class Solution:
def reconstructQueue(self, people):
"""
:type people: List[List[int]]
:rtype: List[List[int]]
"""
ans = []
if not people:
return ans
h2mans = {}
heights = []
for i in range(len(people)):
h, k = people[i]
if h in h2mans:
h2mans[h].append((k, i))
else:
h2mans[h] = [(k, i)]
heights.append(h)
heights.sort()
for height in heights[::-1]:
for k, i in sorted(h2mans[height]):
ans.insert(k, people[i])
return ans