Algorithm/COS
[COS Pro 1급, Python] 6차 3번 : 큰수와 작은수의 차이
JMcunst
2022. 2. 28. 17:45
728x90
문제 유형
코딩
난이도
normal
Note
1. combination(조합) 으로 해결.
2. for문을 돌면서 min과 max를 구해서 빼고 결과를 answer에 업데이트.
Code
# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean
import itertools
def solution(arr, K):
answer = 10001
list_a = list(itertools.combinations(arr, K))
for nums in list_a:
list_nums = list(set(nums))
max_n, min_n = max(list_nums), min(list_nums)
rtn = max_n - min_n
if rtn < answer:
answer = rtn
return answer
arr = [9, 11, 9, 6, 4, 19]
K = 4
ret = solution(arr, K)
print("solution 함수의 반환 값은", ret, "입니다.")
※ 가끔 코드 중 print(~)가 있습니다. 정리 못한 점 죄송합니다.
728x90