diff --git a/workbook_8708/1927-s2.py b/workbook_8708/1927-s2.py new file mode 100644 index 0000000..acc4964 --- /dev/null +++ b/workbook_8708/1927-s2.py @@ -0,0 +1,35 @@ +# 최소 힙 + +import sys +import heapq + +input = sys.stdin.readline + +def solution(): + n = int(input().rstrip()) + h = [] + + for i in range(n): + v = int(input().rstrip()) + if v == 0: + if len(h) == 0: + print(0) + continue + print(heapq.heappop(h)) + else: + heapq.heappush(h, v) + return + +solution() + +""" +걸린 시간: 5분 + +시간 복잡도: heappush와 heappop 모두 현재 트리의 높이 즉, logn만큼 든다. +트리 길이가 점점 길어져서 n까지 가는 것이기 때문에 평균적으로 O(logn)이다. +n번 진행하기 때문에 전체 시간복잡도는 O(nlogn)이다. + +해설: 대놓고 힙 써서 구현하라고 해서 쉬웠다. +0일때 pop하는데, 배열 길이가 0이면 0 출력하도록 설정 +넣을때는 heappush, 뺄때는 heappop 하면 끝 +""" \ No newline at end of file