일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- API
- 최단경로
- 백준
- codility
- counting elements
- Dijkstra
- 2981
- 알고리즘
- error
- 문자열
- applicationeventpublisher
- spring security
- Python
- BFS
- brute force
- HTTP
- Spring
- Greedy
- springboot
- 탐욕법
- algorithm
- beandefinitionstoreexception
- javascript
- 소수
- 프로그래머스
- 라이브템플릿
- java
- 코딩테스트
- 파이썬
- 2018 KAKAO BLIND RECRUITMENT
Archives
- Today
- Total
Altiora Petamus
블랙잭 본문
2798번: 블랙잭
첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장
www.acmicpc.net
🤔생각해보기
모든 경우의 수를 구한 후, 조건을 초과하지 않는 가장 큰 합을 출력하면 되는 간단한 문제이다.
for 문을 돌면서 3가지 카드 '조합'을 구해도 되겠지만, 파이썬 모듈 itertools
를 활용하면 복잡한 과정을 생략할 수 있다.
코드
from itertools import combinations
n, m = map(int, input().split())
cards = list(map(int, input().split()))
result = []
candidates = combinations(cards, 3)
for candidate in candidates:
if sum(candidate) <= m:
result.append(sum(candidate))
print(max(result))
'1day-1algorithm' 카테고리의 다른 글
베스트 앨범 (0) | 2021.06.26 |
---|---|
단어 변환 (0) | 2021.06.25 |
통계학 (0) | 2021.06.22 |
토마토 (0) | 2021.06.19 |
소수 구하기 (에라토스테네스의 체) (0) | 2021.06.18 |