Leetour - 494 targets and (dp)

Leetour-494 targets and

1. Topic

You are given an integer array nums and an integer target .

Add '+' or '-' to each integer in the array , and then concatenate all the integers to construct an expression:

For example, nums = [2, 1], you can add '+' before 2, add '-' before 1, and then concatenate to get the expression "+2-1".
Returns the number of distinct expressions that can be constructed by the above methods that evaluate to target .

Example 1:

Input: nums = [1,1,1,1,1], target = 3
Output: 5
Explanation: There are 5 ways to make the final target sum to 3.

-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3

2. Analysis

  1. topic . First of all, when we saw this topic, what we thought of was the backtracking method , to find all combinations through exhaustive violence, and backtrack the trilogy. But backtracking is often an exponential time complexity, so it often times out, and this question cannot be selected, so we can use dynamic programming 01 knapsack problem from it.

  2. 01 backpack problem . First of all, we have to know the capacity of our knapsack. Maybe we thought that the target was the capacity of the knapsack problem at first, and then we came to find it, but this is the value of multiple nums[i] closest to the target, but it is different from our problem. The resulting 不同表达式的数目output is inconsistent. So we have to change our thinking and redefine it.

    First of all, we can analyze that in this "+" or "-" process, if we want to get the target, it is the sum part - minus part = target , and the sum part + minus part = sum , so we can get the sum part - (sum - sum part) = target , ie sum part = (sum+target)/2 . So far, we know that sum and target are constants, that is, the sum and part are also known.

  3. dynamic equation . We can know and part of it can be obtained from the above , that is, the calculated part and value we can get from it, and set it to the size of the backpack capacity we need. dp[i] represents the number of different expressions when the partial sum is i. If the knapsack capacity size is 5, then when nums[i] is 1, there are dp[4] different methods, because it is All quantities, so it should be a process of accumulation. So we can get our recurrence formula dp[j] += dp[j - nums[i]].

  4. boundary value . As example 1, when we have dp[1], our recursive formula needs the value of dp[0], we can try several times, from which we can get the dp[0]=1 we need . Then use the one-dimensional dp array to solve the 01 knapsack problem.

  5. matches the value . Our sum part is obtained by (sum+target)/2, so our sum part should be an integer, because all the numbers in the title should be integers , so we can add a judgment here, and return 0 directly if it does not match. Secondly, we should also consider whether this integer value is a negative number, and then whether it conforms to the value result we can find.

  6. Write code.

3. Code and Analysis

class Solution {
    
    
    public int findTargetSumWays(int[] nums, int target) {
    
    
        // 1.根据这题首先联想到回溯,其次想到动态规划dp,01背包问题
        // 2.设和为x,减为sum-x,有sum和target,可得x=(sum+target)/2
        // 3.dp[i]表示运算的 结果等于target 的 不同数量
        int sum = 0;
        for (int i = 0; i < nums.length; i++){
    
    
            sum += nums[i];
        }
        if (target > sum || (sum + target) % 2 == 1) return 0;
        if (target < 0 && sum < -target) return 0;
        int size = (sum + target) / 2;
        if (size < 0) size = -size;
        int[] dp = new int[size + 1];
        dp[0] = 1;

        for (int i = 0; i < nums.length; i++){
    
    
            for (int j = size; j >= nums[i]; j--){
    
    
                dp[j] += dp[j - nums[i]];
            }
        }

        return dp[size];
        
    }
}

4. Practice

Leetcode topic link: https://leetcode.cn/problems/target-sum/

Guess you like

Origin blog.csdn.net/qq_51326491/article/details/129295045
DP