关于动态规划的问题494_LEETCODE_TARGET_SUM
在做这道题的时候,思考了很久一直不知道怎么做,如果全部遍历的话肯定会出现TLE超时问题,为什么呢?
1、每个值都可以是取或者不取,那么就有2^n^组合方法,则时间复杂度O(2^n^),随着数量的增长,时间成指数级增长
2、所以放弃了遍历想法
于是乎考虑了动态规划
动态规划:
1、起始和的字典 dp[0] = {0:1}
2、第1个数+-1,此时和的字典为 dp[1] = {1:1,-1:1}
3、第2个数+-1,此时和的字典为 dp[2] = {2:1,0:2,-2:1}
4、第3个数+-1,此时和的字典为 dp[3] = {3:1,1:3,-1:3,-3:1},target为1时,那么返回dp[3][1],即3
比如dp[3][1] = dp[2][2]+dp[2][0] = 1 + 2 = 3
和字典的动态转义方程 dp[i][j] = dp[i-1][j-num] + dp[i-1][j+num]
class Solution: def findTargetSumWays(self, nums, S): """ :type nums: List[int] :type S: int :rtype: int """ # 动态规划: # 1、起始和的字典 dp[0] = {0:1} # 2、第1个数+-1,此时和的字典为 dp[1] = {1:1,-1:1} # 3、第2个数+-1,此时和的字典为 dp[2] = {2:1,0:2,-2:1} # 4、第3个数+-1,此时和的字典为 dp[3] = {3:1,1:3,-1:3,-3:1},target为1时,那么返回dp[3][1],即3 # 比如dp[3][1] = dp[2][2]+dp[2][0] = 1 + 2 = 3 # 和字典的动态转义方程 dp[i][j] = dp[i-1][j-num] + dp[i-1][j+num] sums = 0 for n in nums: sums += n sum_diff = sums - S if sum_diff < 0 or sum_diff % 2 != 0: return 0 dp = {0: 1} for n in nums: temp = {} for sign in (1, -1): for j in dp: key = j + n * sign if key not in temp: value = dp[j] else: value = temp[key] + dp[j] temp[key] = value dp = temp return dp[S] if __name__ == '__main__': s = Solution() a = s.findTargetSumWays([1,1,1,1,1], 3) print(a)