Published:
Updated:

  • insert๋ฌธ์—์„œ person[1]๋ฒˆ์งธ index์— ๋„ฃ๋Š” ์ด์œ ๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด ๋ด์•ผ ๋จ
    • ๊ทธ๋ƒฅ ๋‹จ์ˆœํ•˜๊ฒŒ ๋ฌด์—‡์„ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ์ƒ๊ฐ


SolutionPermalink

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


ReferencePermalink

Leave a comment