LeetCode 416. Partition Equal Subset Sum

来源:互联网 发布:linux mv 移动文件夹 编辑:程序博客网 时间:2024/05/17 09:37

416. Partition Equal Subset Sum

一、问题描述

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:

  1. Each of the array element will not exceed 100.
  2. The array size will not exceed 200.

二、输入输出

Example 1:

Input: [1, 5, 11, 5]Output: trueExplanation: The array can be partitioned as [1, 5, 5] and [11].

Example 2:

Input: [1, 2, 3, 5]Output: falseExplanation: The array cannot be partitioned into equal sum subsets.

三、解题思路

动态规划-Bottom-up

  • 这道题可以转化成在一个数组中找一个组合,让其和为原数组元素和的一半。
  • 用动态规划的话,用dp[i]来表示数组中是否存在和为i的子序列。数组总和一半为target,那么i in range(0, nums.size()-1) 然后更新dp[nums[i]] 到 dp[target]
  • 状态转移方程为:dp[j] = dp[j] || dp[j-nums[i]] for j range in [nums[i], target]
  • 这种Bottom-up的方法一直学不会,感觉思路并不直观,看了答案也不是很清楚。倒是自顶向下带备忘的方法很喜欢,缺点是很多时候搜索过的问题的cache不知道怎么记录。map.insert效率是很低的,虽然时间复杂度一样,实际运行的却很慢,经常超时
class Solution {public:    bool canPartition(vector<int>& nums) {        if(nums.size() == 0) return true;        int sum = accumulate(nums.begin(), nums.end(), 0);        if(sum % 2 != 0) return false;        int target = sum / 2;        vector<bool> dp(target+1, false);        dp[0] = true;        for (int i = 0, n = nums.size(); i < n; ++i) {            for (int j = target; j >= nums[i]; --j) {                dp[j] = dp[j] || dp[j-nums[i]];            }        }        return dp[target];    }};

动态规划-自顶向下带备忘

  • 感觉这个思路很直观,但是没有AC,原因是超时了。因为cache我不知道改怎么加,需要记录数组里具体的元素以及要找的target
  • 同样先将问题转换成在数组中找子序列,使得和为原数组总和的一半。猜测第一个数选择的下标是i 那么一共有n种选法(i range in [0, nums.size()-1]) 那么问题就转化为在去掉nums[i]之后的数组中找和为target-nums[i]的子问题。这个问题和原问题是同型的,所以可以递归调用了。
  • 这份代码之所以超时是由于没有加cache,只是原始的递归,所以复杂度是指数级别的。如果使用map来做cache key要取成数组,插入的效率之前测试着挺慢的,如果大家有什么好办法来做memorization还请不吝赐教
    bool DP_canPartition(vector<int>&nums, int sum)    {        if(nums.size() == 1) return (sum == nums[0]);        for (int i = 0, n = nums.size(); i < n; ++i) {            vector<int> subV = nums;            int subSum = sum - nums[i];            if(subSum == 0) return true;            else if(subSum < 0)continue;            subV.erase(subV.begin()+i);            if(DP_canPartition(subV, subSum))return true;        }        return false;    }    bool canPartition(vector<int>& nums) {        int sum = accumulate(nums.begin(), nums.end(), 0);        if(sum % 2 == 1) return false;        return DP_canPartition(nums, sum / 2);    }
原创粉丝点击