[LeetCode] Target Sum 目标和

题目

  • You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.
    Find out how many ways to assign symbols to make sum of integers equal to target S.

  • Example 1:
    Input: nums is [1, 1, 1, 1, 1], S is 3.
    Output: 5

  • Explanation:
    -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
    There are 5 ways to assign symbols to make the sum of nums be target 3.
  • Note:
    The length of the given array is positive and will not exceed 20.
    The sum of elements in the given array will not exceed 1000.
    Your output answer is guaranteed to be fitted in a 32-bit integer.

分析题目

  • 本题给了我们一个数组,和一个目标值,让我们给数组中每个数字加上正号或负号,然后求和要和目标值相等,求有多少中不同的情况。
  • 首先想到递归来解,尝试在给定的nums数组中的每个位置放置+和 - 符号,并找出导致所需结果S的赋值。我们从第一个数字,调用递归函数calculate(nums,i,sum,S),在递归函数中,分别对累加和sum进行加上当前数字调用递归,和减去当前数字调用递归,这样会涵盖所有情况,并且当所有数字遍历完成后,我们看若累加和等于S了,则结果count自增1.如解法一
  • 记忆化搜索,剪枝操作。可以很容易地观察到,在最后一种方法中,与当前索引相同的i值和与当前总和相同的sum值进行了大量冗余函数调用,因为可以通过多个获得相同的值递归树中的路径。为了消除这种冗余,我们还使用了memoization来存储之前计算过的结果。因此,对于每次计算的调用(nums,i,sum,S),我们存储在memo [i] [sum + 1000]中获得的结果。系数1000被添加为和值的偏移量,以将所有可能的总和映射到正整数范围。通过使用memoization,我们可以在很大程度上修剪搜索空间。(zz)
  • DP

解法一 Brute Force

Complexity - Time: O(2n), Space: O(n)

public class Solution {
    int count = 0;
    public int findTargetSumWays(int[] nums, int S) {
        calculate(nums, 0, 0, S);
        return count;
    }
    public void calculate(int[] nums, int i, int sum, int S) {
        if (i == nums.length) {
            if (sum == S)
                count++;
        } else {
            calculate(nums, i + 1, sum + nums[i], S);
            calculate(nums, i + 1, sum - nums[i], S);
        }
    }
}

解法二 记忆化搜索算法

Complexity - Time: O(l∗n). The memo array of size l*n has been filled just once. Here, l refers to the range of sum and n refers to the size of num array.
Space: O(n)

public class Solution {
    int count = 0;
    public int findTargetSumWays(int[] nums, int S) {
        int[][] memo = new int[nums.length][2001];
        for (int[] row: memo)
            Arrays.fill(row, Integer.MIN_VALUE);
        return calculate(nums, 0, 0, S, memo);
    }
    public int calculate(int[] nums, int i, int sum, int S, int[][] memo) {
        if (i == nums.length) {
            if (sum == S)
                return 1;
            else
                return 0;
        } else {
            if (memo[i][sum + 1000] != Integer.MIN_VALUE) {
                return memo[i][sum + 1000];
            }
            int add = calculate(nums, i + 1, sum + nums[i], S, memo);
            int subtract = calculate(nums, i + 1, sum - nums[i], S, memo);
            memo[i][sum + 1000] = add + subtract;
            return memo[i][sum + 1000];
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值