[백준] 1182번 부분수열의 합

거북이·2023년 1월 25일
0

백준[실버2]

목록 보기
16/81
post-thumbnail

💡문제접근

  • 크기가 양수인 부분수열 즉, 원소의 개수가 최소 1개 이상인 모든 부분수열을 combination을 이용해서 찾은 다음 합이 S가 되는지 판단해 만족하는 부분수열의 개수를 출력하는 코드를 작성했다.

💡코드(메모리 : 30616KB, 시간 : 356ms)

from itertools import combinations
import sys
input = sys.stdin.readline

N, S = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))

cnt = 0
for i in range(1, N+1):
    for j in combinations(arr, i):
        if sum(j) == S:
            cnt += 1
print(cnt)

💡소요시간 : 5m

0개의 댓글