-
[Python] BOJ 2798 블랙잭코딩테스트/백준 2025. 3. 26. 16:47
총 걸린 시간
1631-1641
요구사항
- 시간 제한: 1초
- 메모리 제한: 128MB
- N장의 카드 중 3장의 카드의 합이 최대한 M과 가까운 숫자들의 합을 구해라.
- 3장을 찾을 수 있는 경우만 입력으로 주어진다.
설계
- combinations 라이브러리를 불러와 3장의 조합을 구한다.
- 조합을 돌며 조합의 합이 M 보다 작을 때, 체크하며 가장 큰 값을 구한다.
구현
from itertools import combinations import sys input = lambda : sys.stdin.readline().rstrip() N, M = map(int, input().split()) numbers = map(int, input().split()) num_list = list(combinations(numbers, 3)) max_num = 0 for combination in num_list: if sum(combination) <= M: max_num = max(max_num, sum(combination)) print(max_num)
'코딩테스트 > 백준' 카테고리의 다른 글
[Python] BOJ 2562 최댓값 (6) 2025.04.22 [Python] BOJ 2739 구구단 (0) 2025.04.22 [Python] BOJ 2231 분해합 (0) 2025.03.26 [Python] BOJ 9935 문자열 폭발 (0) 2025.02.20 [Python] BOJ 1406 에디터 (0) 2025.02.20