-
Notifications
You must be signed in to change notification settings - Fork 0
/
타겟넘버_lv2.py
56 lines (31 loc) · 1016 Bytes
/
타겟넘버_lv2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 풀이:
# recursive function을 사용하여 DFS 트리를 구현. 트리의 자식은 더하기 빼기만 있기 때문에
# 모든 조합을 검색해 값을 구한다.
def solution(numbers, target):
cnt = 0
def function(numbers, target, idx):
if idx < len(numbers):
numbers[idx] *= 1
function(numbers, target, idx+1)
numbers[idx] *= -1
function(numbers, target, idx+1)
else:
if sum(numbers) == target:
nonlocal cnt
cnt += 1
function(numbers, target, 0)
return cnt
# 다른 방법
answer = 0
def dfs(numbers, idx, target):
global answer
if idx == len(numbers):
if sum(numbers) == target:
answer += 1
return
dfs(numbers, idx+1, target)
numbers[idx] *= -1
dfs(numbers, idx+1, target)
def solution(numbers, target):
dfs(numbers,0, target)
return answer