Programming/ํ๋ก๊ทธ๋๋จธ์ค
230221 [ํ๋ก๊ทธ๋๋จธ์ค ํ๊ฒ ๋๋ฒ]
ํด๋์๊ทธ
2023. 9. 2. 17:05
๋ฐ์ํ
์ฌ๊ท๋ฅผ ์ด์ฉํ DFS๋ก ํ์๋๋ฐ ํธ์ถ ๋ถ๋ถ์ด ์ดํด๊ฐ ์ ์๋จ ใ
๋ค์ ๋ด์ผ๊ฒ ์
import sys
sys.setrecursionlimit(1000000)
def solution(numbers, target):
n = len(numbers)
cnt = 0
def DFS(index, sum):
if index == n:
if sum == target:
nonlocal cnt
cnt+=1
return
else:
DFS(index+1, sum+numbers[index])
DFS(index+1, sum-numbers[index])
DFS(0,0)
return cnt
๋ฐ์ํ