实际问题的算法抽象——动态规划中的01背包问题

  LeetCode的许多题目都不会明确告诉你用哪一类算法进行求解,甚至可能都无须用复杂算法。然而能明确用哪一类算法解决问题,将对问题的解决提供事半功倍的效果。当然有些题目即使告诉你用哪一类算法,然而如何将该问题抽象成一个数学算法,也是需要锻炼的。这里结合LeetCode 494和416给大家做一点分析。

数学抽象——LeetCode 494

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.

  摆脱跳跃性大脑思维,按照电脑的“笨”办法,我初步想法如下:
  1. 先看所给目标S是否在int数组元素的正和和负和范围内,如果不是的话,不可行。如果在范围内,进行下一步;
  2. 每一个数都有正负两种可能,对每一个数都进行两种判断,总共有2的n次方中可能,看看其中有几种可以即刻。

  明显上述是一个理论可行,实际时间复杂度太高的方法。因此需要进一步的优化。这里需要对问题进行数学加工。
  引用LeetCode的Discuss中的一个经典答案。我们把加上符号后的序列分为正子序列P和负子序列N。则可以得到以下公式。

         sum(P) - sum(N) = target
sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N)
            2 * sum(P) = target + sum(nums)

  是不是瞬间明朗很多了呢。也就是说我们在保证序列和在目标范围内(sum(nums)<|target|)且序列和与目标的和是偶数((target+sum(nums))%2 == 0)的前提下,只需要找到一个正子序列P,使得 sum(P) = (target + sum(nums)) / 2 即可。
  这是第一步,第二步则是找到这个正子序列P。
  给定一个序列nums,给定一个目标和sum(P),求是否能从nums中取出一定的元素,使其和为sum(P)。这个问题是不是很眼熟呢?没错,就是动态规划中的01背包问题。问题分析至此也就十分明朗了。
  代码如下:

public int findTargetSumWays(int[] nums, int s) {
    int sum = 0;
    for (int n : nums)
        sum += n;
    return sum < s || (s + sum) % 2 > 0 ? 0 : subsetSum(nums, (s + sum) >>> 1); 
}   

//01背包问题
public int subsetSum(int[] nums, int s) {
    int[] dp = new int[s + 1]; 
    dp[0] = 1;
    for (int n : nums)
        for (int i = s; i >= n; i--)
            dp[i] += dp[i - n]; 
    return dp[s];
} 

  暂时先让我们看一下以下关于01背包问题的阐述与思路。

动态规划之01背包问题——LeetCode 416

Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Note:
Each of the array element will not exceed 100.
The array size will not exceed 200.

  这个问题还是比较明确的,给定一个正整数序列nums,给定一个目标和sum(nums)/2,求是否能从nums中取出一定的元素,使其和为sum(nums)/2。
  对于01背包问题,引用一个我感觉还不错的讲解 http://blog.csdn.net/xiaoquantouer/article/details/53454346
  最关键的就是记住01背包问题的状态转移公式:f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]},背景如下:有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i],f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。
  模板伪代码:
  伪代码:
  for i = 1 … N
    for v = V…0
      f[v] = max( f[v], f[v-c[i]] + w[i])
  根据这个公式以及上述网页的阐述,这道题的解法如下链接所示 LeetCode 416
  优化时间复杂度后,如下:

public boolean canPartition(int[] nums) {
    int sum = 0;

    for (int num : nums) {
        sum += num;
    }

    if ((sum & 1) == 1) {
        return false;
    }
    sum /= 2;

    int n = nums.length;
    boolean[] dp = new boolean[sum+1];
    Arrays.fill(dp, false);
    dp[0] = true;

    for (int num : nums) {
        for (int i = sum; i > 0; i--) {
            if (i >= num) {
                dp[i] = dp[i] || dp[i-num];
            }
        }
    }

    return dp[sum];
}

  这里的关键部分是如下这段:

    for (int num : nums) {
         for (int i = sum; i > 0; i--) {
             if (i >= num) {
                 dp[i] = dp[i] || dp[i-num];
             }
         }
     }

     return dp[sum];

  这里等号左边的dp[i]代表当容量为i时是否能满足要求(等同于01背包问题中的获得最大价值f[v])。等号右边的dp[i]代表将num这一项放入背包时是否能满足要求,dp[i-num]代表将num这一项放入背包时是否能满足要求。对于每一项物品num,都进行判断,判断其在背包剩余容量从sum到num时的要求满足情况。满足的情况保存下来,留给下一项物品num,作为判断的基础,即在下一项num判断时,有一些背包容量(可能容量还未达到sum,但这是为最终的背包容量sum做铺垫)已经满足要求了。

01背包问题 根据具体情境进行调整——LeetCode 494【续】

  刚才这一题代码的01背包问题部分如下:

public int findTargetSumWays(int[] nums, int s) {
    int sum = 0;
    for (int n : nums)
        sum += n;
    return sum < s || (s + sum) % 2 > 0 ? 0 : subsetSum(nums, (s + sum) >>> 1); 
}   

//01背包问题
public int subsetSum(int[] nums, int s) {
    int[] dp = new int[s + 1]; 
    dp[0] = 1;
    for (int n : nums)
        for (int i = s; i >= n; i--)
            dp[i] += dp[i - n]; 
    return dp[s];
} 

  这个与LeetCode 416有点区别。416初始化时是一个个boolean值(是否满足要求),这里初始化时是整形值(是否塞满背包)。但套用的公式都一样,记住公式即可。
  值得注意的是这两道题其实都只有不同物品消耗的背包容量,而没有他们提供的价值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值