from collections import deque
def solution(numbers, target):
answer = 0
queue = deque()
n = len(numbers)
queue.append([numbers[0],0])
queue.append([-1*numbers[0],0])
while queue:
temp, idx = queue.popleft()
idx += 1
if idx < n:
queue.append([temp+numbers[idx], idx])
queue.append([temp-numbers[idx], idx])
else:
if temp == target:
answer += 1
return answer
다른사람 풀이 3
from itertools import product
def solution(numbers, target):
l = [(x, -x) for x in numbers]
s = list(map(sum, product(*l)))
return s.count(target)
다른사람 풀이 4
def solution(numbers, target):
q = [0]
for n in numbers:
s = []
for _ in range(len(q)):
x = q.pop()
s.append(x + n)
s.append(x + n*(-1))
q = s.copy()
return q.count(target)