1927: 최소 힙 && 11279: 최대 힙 && 11286: 절댓값 힙

ewillwin·2023년 6월 14일
0

Problem Solving (BOJ)

목록 보기
66/230

1927: 최소 힙

import sys
import heapq

N = int(input())
heap = []
for _ in range(N):
	cmd = int(sys.stdin.readline()[:-1])
	if cmd == 0:
		if len(heap) == 0:
			print(0)
		else:
			print(heapq.heappop(heap))
	else:
		heapq.heappush(heap, cmd)
  • python의 heapq 묘듈을 사용하여 구현

11279: 최대 힙

import sys
import heapq

N = int(input())
heap = []
for _ in range(N):
    cmd = int(sys.stdin.readline()[:-1])
    if cmd == 0:
        if len(heap) == 0:
            print(0)
        else:
            print(-heapq.heappop(heap))
    else:
        heapq.heappush(heap, -cmd)

11286: 절댓값 힙

import sys
import heapq

N = int(input())
heap = []
for _ in range(N):
    cmd = int(sys.stdin.readline()[:-1])
    if cmd == 0:
        if len(heap) == 0:
            print(0)
        else:
            tmp = heapq.heappop(heap)
            print(tmp[1])
    else:
        heapq.heappush(heap, [abs(cmd), cmd])
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글