Skip to content

Commit

Permalink
2024-05-28 가운데를 말해요
Browse files Browse the repository at this point in the history
  • Loading branch information
seongwon030 committed May 28, 2024
1 parent 0d5e4b2 commit a0f7a9e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions seongwon030/큐/가운데를말해요.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import heapq
import sys

input = sys.stdin.readline

max_heap = []
min_heap = []
def push_num(num):
# max_heap과 min_heap 크기 같으면 max_heap에 넣음
if len(max_heap) == len(min_heap):
heapq.heappush(max_heap, -num)
else:
# 크기가 다르면 이전에 max_heap에 넣은 것이므로
# min_heap에 넣는다
heapq.heappush(min_heap, num)

if min_heap and -max_heap[0] > min_heap[0]:
min = heapq.heappop(min_heap)
max = -heapq.heappop(max_heap)
heapq.heappush(max_heap, -min)
heapq.heappush(min_heap, max)

n = int(input())
for i in range(n):
push_num(int(input()))
print(-max_heap[0])

0 comments on commit a0f7a9e

Please sign in to comment.