416. Partition Equal Subset Sum

Description

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: true

Explanation: The array can be partitioned as [1, 5, 5] and [11].

Example 2:

Input: [1, 2, 3, 5]

Output: false

Explanation: The array cannot be partitioned into equal sum subsets.

Solution

就是01背包问题。

DP, time O(l * n), space O(l * n)

class Solution {
    public boolean canPartition(int[] nums) {
        int sum = getSum(nums);
        if (sum % 2 > 0) {
            return false;
        }
    
        int n = nums.length;
        boolean[][] dp = new boolean[n + 1][sum + 1];
        dp[0][0] = true;
        
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= sum; ++j) {
                dp[i][j] = dp[i - 1][j];
                
                if (j >= nums[i - 1]) {
                    dp[i][j] |= dp[i - 1][j - nums[i - 1]];   
                }
            }
        }
        
        
        return dp[n][sum / 2];
    }
    
    public int getSum(int[] nums) {
        int sum = 0;
        
        for (int n : nums) {
            sum += n;
        }
        
        return sum;
    }
}

DP, time O(n), space O(n)

dp数组可以降成一维,从后往前遍历即可。

class Solution {
    public boolean canPartition(int[] nums) {
        int sum = getSum(nums);
        return sum % 2 == 0 && subsetSum(nums, sum, sum / 2) ? true : false;
    }
    
    public int getSum(int[] nums) {
        int sum = 0;
        
        for (int n : nums) {
            sum += n;
        }
        
        return sum;
    }
    
    public boolean subsetSum(int[] nums, int sum, int target) {
        boolean[] dp = new boolean[sum + 1];
        dp[0] = true;
        
        for (int n : nums) {
            for (int i = sum; i >= n; --i) {
                dp[i] |= dp[i - n];
            }
        }
        
        return dp[target];
    }
}

你可能感兴趣的:(416. Partition Equal Subset Sum)