문제풀이
2022.07.04 - [공부기록/자바스크립트 코딩테스트] - [백준/Python] 2696번 중앙값 구하기
어제 공부했던 중앙값 구하기를 다시 복습하고 풀었습니다.
로직은 동일합니다.
import sys
import heapq
input = sys.stdin.readline
maxh = []
minh = []
num = int(input())
first = int(input())
heapq.heappush(maxh,-1*first)
print(first)
for i in range(1,num) :
target = int(input())
if (i+1) % 2 == 0 :
if -1*maxh[0] < target :
heapq.heappush(minh,target)
else :
heapq.heappush(minh,-1*heapq.heappop(maxh))
heapq.heappush(maxh,-1*target)
# 출력
print(-1*maxh[0])
else :
if minh[0] < target:
heapq.heappush(maxh,-1*heapq.heappop(minh))
heapq.heappush(minh,target)
else :
heapq.heappush(maxh,-1*target)
# 출력
print(-1*maxh[0])
'공부기록 > 자바스크립트 코딩테스트' 카테고리의 다른 글
if문 중첩에 관한 고찰 (0) | 2022.07.06 |
---|---|
[백준/JS] 2753 윤년 (0) | 2022.07.06 |
[백준/Python] 2075번 N번째 큰수 (0) | 2022.07.05 |
[백준/11286] 11286 절대값 힙 (0) | 2022.07.05 |
[백준/Python] 11279 최대힙 (0) | 2022.07.04 |