[파이썬][백준 2798번] 블랙잭
1. 문제Permalink
[Bronze II] 블랙잭 - 2798Permalink
성능 요약Permalink
메모리: 30840 KB, 시간: 160 ms
분류Permalink
브루트포스 알고리즘(bruteforcing)
출처: 백준, https://https://www.acmicpc.net/
2. 해결방법 시간복잡도Permalink
- 브루트 포스 O(N^3)
3. 문제 해결 및 코드Permalink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N, M = map(int, input().split()) | |
H = list(map(int, input().split())) | |
result = 0 | |
for i in range(N): | |
for j in range(i + 1, N): | |
for h in range(j + 1, N): | |
if (H[i] + H[j] + H[h]) > M: | |
continue | |
else: | |
result = max(result, H[i] + H[j] + H[h]) | |
print(result) |
-
주석을 참고하면서 이해를 돕습니다.Permalink
4. 알고리즘 및 해설Permalink
- 해당 문제의 경우 분해합을 구하는 문제로 브루트 포스를 통해 간단하게 해결하면 된다.
- 반복문을 통해 해당 위치값 다음 위치값 다다음 위치값을 더해준 값이 M 값보다 크다면 반복문을 계속 이어준다.
- H(i) + H(i+1) + H(i+2)
- 아니라면 결과값은 현재 결과값과 세 값의 합중 비교하여 더 큰값으로 변경한다.
- 최종 결과값을 출력해준다.