728x90
기본 아이디어
dfs, 다음 숫자 더하기, 빼기를 모두 스택에 담기
def solution(numbers, target):
stack = [(0, 0)] # index, current_sum
count = 0
while stack:
index, current_sum = stack.pop()
if index == len(numbers):
if current_sum == target:
count += 1
else:
stack.append((index + 1, current_sum + numbers[index]))
stack.append((index + 1, current_sum - numbers[index]))
return count
- 머릿속에서 인덱스가 전혀 안 돈다!! 큰일낫당
728x90
'Programming > Python' 카테고리의 다른 글
[프로그래머스] 게임 맵 최단거리 (0) | 2025.04.07 |
---|---|
[프로그래머스] 더 맵게 (0) | 2025.04.02 |
[프로그래머스] K번째 수 (1) | 2025.04.02 |
[프로그래머스] 체육복 (0) | 2025.03.27 |
[프로그래머스] 최소직사각형 (1) | 2025.03.27 |