[리트코드] Queue Reconstruction by Height

박형진·2021년 12월 31일
0

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


1. 전체 코드

class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        h = []
        for i in people:
            heapq.heappush(h, (-i[0], i[1]))
        result = []
        while h:
            person = heapq.heappop(h)
            result.insert(person[1], (-person[0], person[1]))
        return result

2. 후기

그리디 문제에서는 최소 또는 최대값이 필요한 경우가 많기 때문에 우선순위 큐 사용을 고려해보자

파이썬의 heapq는 h = (-7, 30), (-7, 23) heappop(h)을 하면 2330을 비교하여 더 작은 값인 (-7, 23)이 우선적으로 추출된다.

profile
안녕하세요!

0개의 댓글