Published:
Updated:

  • insertλ¬Έμ—μ„œ person[1]번째 index에 λ„£λŠ” 이유λ₯Ό 잘 생각해 봐야 됨
    • κ·Έλƒ₯ λ‹¨μˆœν•˜κ²Œ 무엇을 λ°˜ν™˜ν•˜λŠ”μ§€ 생각


Solution

import heapq
from typing import List


class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        answer: List[List[int]] = []
        heap = []

        for person in people:
            # (κ·Έ μ‚¬λžŒμ˜ ν‚€, μžμ‹ μ˜ ν‚€ 이상인 μ‚¬λžŒλ“€μ˜ 수) -> μ΅œλŒ€νž™μœΌλ‘œ
            # 즉, ν‚€κ°€ 큰 μ‚¬λžŒλΆ€ν„° μ²˜λ¦¬ν•  수 있게 됨
            heapq.heappush(heap, (-person[0], person[1]))

        while heap:
            # ν‚€κ°€ κ°€μž₯ 큰 μ‚¬λžŒμ„ pop
            person = heapq.heappop(heap)
            # person[1]번째 index에 _object μΆ”κ°€
            # μ™œλƒλ©΄ person[1]은 μžμ‹ μ˜ ν‚€ 이상인 μ‚¬λžŒλ“€μ˜ μˆ˜μ΄λ―€λ‘œ index에 μ‚½μž…ν•˜λŠ” κ±°μž„
            answer.insert(person[1], [-person[0], person[1]])

        return answer


Reference

Leave a comment