406. Queue Reconstruction by Height

  • 54.8%

https://leetcode.com/problems/queue-reconstruction-by-height/description/

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.

1
2
3
4
5
6
7
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]]

方法一:


python

175ms, 55.94%, October 18, 2016

https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution(object):
def reconstructQueue(self, people):
"""
:type people: List[List[int]]
:rtype: List[List[int]]
"""
if not people: return []
peopledct, height, res = {}, [], []

for i in xrange(len(people)):
p = people[i]
if p[0] in peopledct:
peopledct[p[0]] += (p[1], i),
else:
peopledct[p[0]] = [(p[1], i)]
height += p[0],
height.sort()

for h in height[::-1]:
peopledct[h].sort()
for p in peopledct[h]:
res.insert(p[0], people[p[1]])
return res
谢谢你,可爱的朋友。