[LeetCode] 416. Partition Equal Subset Sum

来源:互联网 发布:辐射4灵光优化 编辑:程序博客网 时间:2024/06/14 14:45

Problem:

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.

Solution:

class Solution {public:    bool canPartition(vector<int>& nums) {        int sum = accumulate(nums.begin(), nums.end(), 0); // 累加        if (sum % 2 != 0) return false; // 无法平分为两部分        return dpCheck(nums, sum / 2);    }            // 原问题等价于能否找到一个子集,使得子集中所有元素值之和 == sum / 2    bool dpCheck(vector<int>& nums, int target) {        vector<bool> reachable(target + 1, false);        /* reachable[i] == true 表示数组中的元素能够组成值i, == false 表示无法组成i           e.g. reachable[0] == true表示无论如何数组中的元素均可以组成0        */        reachable[0] = true;                for (auto num : nums) {            for (int i = target; i >= num; i--) {                if (reachable[i - num] == true) {                    reachable[i] = true;                }            }        }        return reachable[target];    }};

解题思路分析:

1. 原问题可以转化为找目标子集的问题,若能找到一个子集,使得该子集的所有元素之和为原集合所有

    元素之和的一半,即可以证明原集合可以平分为两个等和的子集;

2. 应用动态编程的思想,定义一个布尔类型的数组reachable,数组的下标i表示数组元素是否能够组成i;

   (举个栗子,reachable[5] == true表示数组元素可以组成5,显然一开始可知,reachable[0] = true,因为无

   论如何0都是可以得到的,只需分配一个空的子集即可);

3. 要确定能否得到目标子集,即要判断 reachable[target] == true ? 

4. 再举个栗子解析DP的过程,以 nums[3] =  {6, 5, 11} 为例:

    为了求解数组元素是否能够组成目标值 target = 11,遍历数组的每一个元素,对每一个元素num,检查在

    该元素的基础上(即reachable[num] == true的基础上),是否能得到 11 。当扫描到元素 5 时,会首先作检查:

    reachable[11-5] 即 reachable[6] == true ?而在第一次扫描时,已经确定了reachable[6] == true即6是可以

    由数组元素得到的,所以在扫描到5时,可以知道在5的基础上可以组成元素11,于是有reachable[11] = true。


解法参考自:https://discuss.leetcode.com/topic/63049/my-simple-c-dp-code-with-comments