N이 주어지고 NxN의 표에 들어갈 각각의 숫자도 주어진다.
전체 NxN개의 숫자 중 N번째 큰수를 출력한다.
메모리 초과 때문에 좀 애먹었다.
heap을 이용하는 문제인거는 바로 알았는데 Max-heap
을 이용해서 N번째 큰수를 출력하다보니 자꾸 메모리 초과 발생
그래서 Min-heap
으로 해서 큐의 길이를 N만큼으로 유지하는 방법으로 해결
import sys
import heapq
input = sys.stdin.readline
n = int(input().strip())
hq = []
for _ in range(n):
nums = list(map(int, input().split()))
if not hq:
for num in nums:
heapq.heappush(hq, num)
else:
for num in nums:
if hq[0] < num:
heapq.heappush(hq, num)
heapq.heappop(hq)
print(hq[0])