영호
[알고리즘] k번째 큰 수 (Python) 본문
문제 설명
1 ~ 100 사이의 자연수 카드 N장 중 3장을 뽑아 합한 수 들 중 K번째로 큰 수를 출력하는 문제입니다.
나의 풀이
- 3장을 뽑을 때 각 카드들의 순서는 중요하지 않기 때문에 itertools의 combination을 사용해 풀었습니다.
- combination을 잘 모르시면 이 글을 참고해주세요.
- 중복 제거를 위해 set자료형을 사용했습니다.
Code
import sys
from itertools import combinations
# sys.stdin=open("input.txt", "r")
n, k=map(int, input().split())
nums = list(map(int, input().split()))
pick_3 = list(combinations(nums,3))
sum_list = [sum(i) for i in pick_3]
not_duple = set(sum_list)
answer = list(not_duple)
answer.sort(reverse=True)
print(answer[k-1])
'Algorithm > 구현' 카테고리의 다른 글
[알고리즘] 대표값 (Python) (0) | 2022.06.07 |
---|---|
[프로그래머스] 로또의 최고 순위와 최저 순위 - Python (0) | 2022.05.11 |
Comments