-
Notifications
You must be signed in to change notification settings - Fork 43
/
target-sum.py
115 lines (108 loc) · 3 KB
/
target-sum.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# V0
# IDEA : DP
# IDEA :
# dp[0][0] = 1;
# dp[i + 1][x + nums[i]] += dp[i][x];
# dp[i + 1][x - nums[i]] += dp[i][x];
# ( dp[i][j] -> at index = i, # of the way can have sum = j )
class Solution:
def findTargetSumWays(self, nums, S):
"""
:type nums: List[int]
:type S: int
:rtype: int
"""
_len = len(nums)
dp = [collections.defaultdict(int) for _ in range(_len + 1)]
dp[0][0] = 1
for i, num in enumerate(nums):
for sum, cnt in dp[i].items():
dp[i + 1][sum + num] += cnt
dp[i + 1][sum - num] += cnt
return dp[_len][S]
# V1
# http://bookshadow.com/weblog/2017/01/22/leetcode-target-sum/
# IDEA : DP
import collections
class Solution(object):
def findTargetSumWays(self, nums, S):
"""
:type nums: List[int]
:type S: int
:rtype: int
"""
dp = collections.Counter()
dp[0] = 1
for n in nums:
ndp = collections.Counter()
for sgn in (1, -1):
for k in dp.keys():
ndp[k + n * sgn] += dp[k]
dp = ndp
return dp[S]
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/80484450
# IDEA : DP
# DP equation
# -> dp[0][0] = 1;
# -> dp[i + 1][x + nums[i]] += dp[i][x];
# -> dp[i + 1][x - nums[i]] += dp[i][x];
# (x : the "sum" can be built at last positon )
class Solution:
def findTargetSumWays(self, nums, S):
"""
:type nums: List[int]
:type S: int
:rtype: int
"""
_len = len(nums)
dp = [collections.defaultdict(int) for _ in range(_len + 1)]
dp[0][0] = 1
for i, num in enumerate(nums):
for sum, cnt in dp[i].items():
dp[i + 1][sum + num] += cnt
dp[i + 1][sum - num] += cnt
return dp[_len][S]
# V1''
# IDEA : DFS (TIME OUT ERROR)
class Solution(object):
def findTargetSumWays(self, nums, S):
"""
:type nums: List[int]
:type S: int
:rtype: int
"""
def helper(index, acc):
if index == len(nums):
if acc == S:
return 1
else:
return 0
return helper(index + 1, acc + nums[index]) + helper(index + 1, acc - nums[index])
return helper(0, 0)
# V1
# https://blog.csdn.net/xiaoxiaoley/article/details/78968852
# dp[x+y] += dp[y]
class Solution(object):
def findTargetSumWays(self, nums, S):
"""
:type nums: List[int]
:type S: int
:rtype: int
"""
if sum(nums)<S:
return 0
if (S+sum(nums))%2==1:
return 0
target = (S+sum(nums))//2
dp = [0]*(target+1)
dp[0] = 1
for n in nums:
i = target
while(i>=n):
dp[i] = dp[i] + dp[i-n]
i = i-1
return dp[target]
# V1'''
# https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-494-target-sum/
# V2