Skip to content

Commit f494b4b

Browse files
committed
[BOJ] 2075 N번째 큰 수 (S3)
1 parent 6de624b commit f494b4b

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

심수연/8주차/260217.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://www.acmicpc.net/problem/2075
2+
3+
import sys
4+
import heapq
5+
6+
input = sys.stdin.readline
7+
8+
N = int(input())
9+
10+
hq = []
11+
12+
for _ in range(N):
13+
for num in map(int, input().split()):
14+
if len(hq) < N:
15+
heapq.heappush(hq, num)
16+
# n번째 수보다 크면 넣기
17+
else: # len(hq) = N 유지
18+
if hq[0] < num: # hq의 최솟값이 num보다 작으면
19+
heapq.heappushpop(hq, num) # num을 hq에 넣고 hq의 최솟값 빼내기
20+
21+
# hq 길이를 N(ex. 5)로 고정해두고, 계속 가장 큰 N개만큼의 값들만 들어가도록 유지시킨 다음, 가장 작은 값(= N번째로 큰 수 = 최소힙) 을 뽑아내는 거임!!
22+
# ex) [35, 41, 48, 49, 52] -> 35
23+
print(hq[0]) # 최소힙

0 commit comments

Comments
 (0)