[LeetCode] Queue Reconstruction by Height

yoonene·2023년 3월 21일
0

알고리즘

목록 보기
61/62

문제 이동
heapq로 푸는 문제
python의 heapq는 최소 값 힙만 제공한다.
따라서 -붙여서 풀어야 함.

import heapq
class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        result, heap = [], []
        for h, o in people:
            heapq.heappush(heap, (-h, o))
        for _ in range(len(heap)):
            h, o = heapq.heappop(heap)
            result.insert(o, (-h, o))
        return result
profile
NLP Researcher / Information Retrieval / Search

0개의 댓글