- 문제 : https://www.acmicpc.net/problem/1182
부분수열이 꼭 기존의 배열 순서를 유지해야한다고 생각해서 헤맸던 문제다.
- 부분수열은 굳이 순서 유지될 필요 없다.
- 길이가 i(1 ~ n)인 배열을 combinations으로 임의로 뽑는다.
- sum(arr)한 값이 s와 같을 때 answer += 1을 해주면 된다.
정답 풀이
from itertools import combinations
# 부분수열은 주어진 배열에서 순서 상관없이 뽑는 것이다?
# 굳이 원래 배열 순서대로 연속될 필요 없음
n, s = map(int, input().split())
arr = list(map(int, input().split()))
answer = 0
for length in range(1, n + 1):
for each in combinations(arr, length):
if sum(each) == s:
answer += 1
print(answer)
'백준' 카테고리의 다른 글
[백준/누적합] 21921번 : 블로그 (0) | 2024.02.15 |
---|---|
[백준/누적합] 21758번 : 꿀 따기 (0) | 2024.02.13 |
[백준/그리디] 20300번 : 서강근육맨 (0) | 2024.02.13 |
[백준/정렬] 2910번 : 빈도 정렬 (2) | 2024.02.12 |
[백준/bfs] 2589번 : 보물섬 (0) | 2024.02.12 |