
문제 분석
- N개의 수가 있을 때 K번째 있는 수를 구하라
정답 코드
1. sort 정렬
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
h = list(map(int, input().split()))
h = sorted(h)
print(h[k-1])
2. heapq
import sys, heapq
input = sys.stdin.readline
n, k = map(int, input().split())
h = list(map(int, input().split()))
heapq.heapify(h)
# 항상 힙의 길이는 k로 유지
ans = []
for _ in range(k) :
ans.append(heapq.heappop(h))
print(ans[-1])
'코딩 테스트 > 백준' 카테고리의 다른 글
[Python] 백준 15989 1,2,3 더하기 4 (0) | 2025.02.13 |
---|---|
[Python] 백준 5397 키로거 (0) | 2025.02.13 |
[Python] 백준 2630 색종이 만들기 (0) | 2025.02.12 |
[Python] 백준 1431 시리얼 번호 (0) | 2025.02.12 |
[Python] 백준 11651 좌표 정렬하기 2 (0) | 2025.02.12 |