- 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