Skip to content

Commit

Permalink
Merge pull request #58 from AlgoLeadMe/14-seongwon030
Browse files Browse the repository at this point in the history
14-seongwon030
  • Loading branch information
seongwon030 authored Jun 30, 2024
2 parents f5ca956 + a0f7a9e commit 8f6a3fa
Showing 1 changed file with 26 additions and 0 deletions.
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 8f6a3fa

Please sign in to comment.