494. Target Sum
Posted tobeabetterpig
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了494. Target Sum相关的知识,希望对你有一定的参考价值。
494. Target Sum https://www.youtube.com/watch?v=r6Wz4W1TbuI https://leetcode.com/problems/target-sum/solution/ ===================== Time complexity : ns . the number of elements is n, n levels , every level has at most s distinct sums Space : ns the explanation for s : Say we have number 11111, then the max we can have is 5 , min is -5 Then at the last level, we can have at most 5 + 5 = 10 distinct sums , 10 is the s in this case If the elements are 1 2 2 3 Then the max is 1 + 2 + 2 + 3 = 8 The min is -1 - 2 - 2 - 3 = - 8 The number of distinct sums can only range from -8 to 8. Then the number of distinct sum is 8 + 8 = 16, 16 is the s in this case. So every time we want the number of sum , we look up the last level , there are at most s positions to look at So the time complexity for each level is s, there are n levels in total, so the time complexity in total is s * n 2d array dp class Solution { public int findTargetSumWays(int[] nums, int S) { int n = nums.length; int sum = 0; for(int i = 0; i < n; i++){ sum += nums[i]; } if(sum < S || S <-sum) return 0; // the max is sum, the min is - sum int[][] ways = new int[n+1][2* sum + 1]; ways[0][sum] = 1; for(int i = 0; i < n; i++){ for(int j = 0; j < 2 * sum + 1; j++){ // we know it can never be out of range if(ways[i][j] > 0){ ways[i + 1][j + nums[i]] += ways[i][j]; ways[i + 1][j - nums[i]] += ways[i][j]; } } } return ways[n][S + sum]; } } 1. in the example 1 1 1 1 1 we dont have negative index so instead of -5 -4 -3 -2 -1 || 0 || 1 2 3 4 5 we use 0 1 2 3 4 || 5 || 6 7 8 9 10 its the same 2. if(ways[i][j] > 0){ ways[i + 1][j + nums[i]] += ways[i][j]; ways[i + 1][j - nums[i]] += ways[i][j]; } j is the current sum. when we found a number (ways[i][j], means the number of ways to have sum j) bigger than 0 in the int[][] ways . we know it can contribute to the next level value. since the number we need to add or subtract soon is nums[i]. so the sum is gonna be j + nums[i] or j - nums[i], and its position is ways[i + 1][j + nums[i]] or ways[i + 1][j - nums[i]] 3 for(int j = 0; j < 2 * sum + 1; j++){ // we know it can never be out of range if(ways[i][j] > 0){ ways[i + 1][j + nums[i]] += ways[i][j]; ways[i + 1][j - nums[i]] += ways[i][j]; Because we know that all results are between sum and -sum In our code its from 0 to 2 * sum - 1 so we don’t need to check for(int j = 0; j < 2 * sum + 1; j++) ========================= 1d array dp Time complexity : ns . the number of elements is n, n levels , every level has at most s distinct sums Space : n class Solution { public int findTargetSumWays(int[] nums, int S) { int n = nums.length; int sum = 0; for(int i = 0; i < n; i++){ sum += nums[i]; } if(sum < S || S <-sum) return 0; int[] dp = new int[2 * sum + 1]; dp[sum] = 1; for(int i = 0; i < nums.length; i++){ int[] next = new int[2 * sum + 1]; for(int k = 0; k < 2 * sum + 1; k++){ if(dp[k] > 0){ next[k + nums[i]] += dp[k]; next[k - nums[i]] += dp[k]; } } dp = next; } return dp[sum + S]; } } ======================= Dfs To do : dfs hua hua shi pin https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-494-target-sum/
以上是关于494. Target Sum的主要内容,如果未能解决你的问题,请参考以下文章