문제
코드
from collections import deque
def solution(numbers: list, target: int) -> int:
answer = 0
queue = deque()
queue.append([-numbers[0], 0])
queue.append([numbers[0], 0])
while queue:
number, index = queue.popleft()
index += 1
if index == len(numbers):
if number == target:
answer += 1
else:
queue.append([number - numbers[index], index])
queue.append([number + numbers[index], index])
return answer
if __name__ == '__main__':
print(solution([1, 1, 1, 1, 1], 3))
print(solution([4, 1, 2, 1], 4))
결과
출처 & 깃허브
programmers
github