python/프로그래머스

프로그래머스 - 타겟 넘버

느리지만 꾸준하게 2022. 3. 29. 16:55
// DFS로 구현

def solution(numbers, target):
    sup = [0]
    for i in numbers:
        sub = []
        for j in sup:
            sub.append(j+i)
            sub.append(j-i)
        sup = sub
    return sup.count(target)

 

 

 

참고 : https://train-validation-test.tistory.com/entry/Programmers-level-2-%ED%83%80%EA%B2%9F-%EB%84%98%EB%B2%84-python

 

[ Programmers ] level 2 - 타겟 넘버 ( python )

코딩테스트 풀이 프로그래머스 level 2 문제 타겟 넘버 - 깊이/너비 우선 탐색(DFS/BFS) 문제 설명 n개의 음이 아닌 정수가 있습니다. 이 수를 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예

train-validation-test.tistory.com