function solution(numbers, target) {
let answer = 0;
const stack = [
[numbers[0], 0],
[-numbers[0], 0],
];
while (stack.length) {
let [sum, index] = stack.pop();
index += 1;
const next = numbers[index];
if (index < numbers.length) {
stack.push([sum + next, index]);
stack.push([sum - next, index]);
} else {
sum === target && answer++;
}
}
return answer;
}
Level
프로그래머스 Lv2