chyam

[프로그래머스 Lv2,python]- 타겟 넘버 본문

프로그래머스/LV2

[프로그래머스 Lv2,python]- 타겟 넘버

chyam_eun 2025. 1. 15. 09:40

https://school.programmers.co.kr/learn/courses/30/lessons/43165

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

def solution(num, target):
    global cnt # 전역변수
    cnt=0 # 개수
    def dfs(sum,n):
        global cnt # 다시 명시해줘야함
        if n==len(num): 
            if target==sum: # 값 비교
                cnt+=1
            return
        dfs(sum+num[n],n+1) # 먼저 더하고
        dfs(sum-num[n],n+1) # 빼기
    dfs(0,0)
    return cnt

 

처음에 생각을 잘못해서 빼는 부분에 n+1을 n-1로 계속해서 안풀렸었다.