🦖 Programming/Python
[Python] ⭐️백준 알고리즘 1655번 : 가운데를 말해요
박낑깡이
2022. 10. 7. 01:23
https://www.acmicpc.net/problem/1655
👩💻문제 이해
👩💻leftheap 과 rightheap의 크기가 같아질 때 중간값 반환 : 성공🌈
import heapq
n = int(input())
leftHeap = []
rightHeap = []
for i in range(n):
num = int(input())
if len(leftHeap) == len(rightHeap):
heapq.heappush(leftHeap, -num)
else:
heapq.heappush(rightHeap, num)
if rightHeap and rightHeap[0] < -leftHeap[0]:
leftValue = heapq.heappop(leftHeap)
rightValue = heapq.heappop(rightHeap)
heapq.heappush(leftHeap, -rightValue)
heapq.heappush(rightHeap, -leftValue)
print(-leftHeap[0])