- 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