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.

Idea 1. Subset sum

[1, 5, 11, 5]

containing 1: [1], sum {1}

containing 5: [5], [1, 5]   sum {5, 6}

containing 11: [11], [1, 11], [5, 11], [1, 5, 11]  {11, 12, 16, 17}

containing: 5: [5], [1, 5], [5, 5], [1, 5, 5], [11, 5], [1, 11, 5], [5, 11, 5], [1, 5, 11, 5], {5, 6, 10, 11, 16, 17, 21, 22}

Time complexity: O(2^n -1)

Space complexity: O(2^n -1)

 class Solution {
public boolean canPartition(int[] nums) {
int totalSum = 0;
for(int num: nums) {
totalSum += num;
}
if(totalSum%2 != 0) {
return false;
} List<List<Integer>> endSum = new ArrayList<>();
for(int i = 0; i < nums.length; ++i) {
List<Integer> curr = new ArrayList<>();
if(nums[i] == totalSum/2) {
return true;
}
curr.add(nums[i]);
for(int j = 0; j < i; ++j) {
for(int val: endSum.get(j)) {
int currSum = val + nums[i];
if(currSum == totalSum/2) {
return true;
}
curr.add(currSum);
}
}
endSum.add(curr);
}
return false;
}
}

Idea 2: dynamic programming. Let dp[i][j] represents if the subset sum from num[0..i] could reach j,

dp[i][j] = dp[i-1][j] not picking nums[i],

    dp[i-1][j-nums[i]] picking nums[i]

Note. to initialise dp[-1][0] = 0

Time complexity: O(n*target)

Space complexity: O(n*target)

 class Solution {
private void backtrack(int[] nums, int i, boolean[][] dp, int target) {
if(i > nums.length) {
return;
} for(int j = 1; j <= target; ++j) {
dp[i][j] = dp[i-1][j];
if(j >= nums[i-1]) {
dp[i][j] = dp[i][j] || dp[i-1][j-nums[i-1]];
}
}
backtrack(nums, i+1, dp, target);
} public boolean canPartition(int[] nums) {
int totalSum = 0;
for(int num: nums) {
totalSum += num;
} if(totalSum %2 != 0) {
return false;
}
int n = nums.length;
int target = totalSum/2;
boolean[][] dp = new boolean[n+1][target+1];
for(int i = 0; i <= n; ++i) {
dp[i][0] = true;
} backtrack(nums, 1, dp, target);
return dp[n][target];
}
}
 class Solution {
public boolean canPartition(int[] nums) {
int totalSum = 0;
for(int num: nums) {
totalSum += num;
} if(totalSum %2 != 0) {
return false;
} int target = totalSum/2;
int m = nums.length;
boolean[][] dp = new boolean[m+1][target+1];
dp[0][0] = true; for(int i = 1; i <= m; ++i) {
for(int j = 1; j <= target; ++j) {
dp[i][j] = dp[i-1][j];
if(j >= nums[i-1]) {
dp[i][j] = dp[i][j] || dp[i-1][j-nums[i-1]];
}
}
} return dp[m][target];
}
}

Idea 2. dynamic programming, 二维到一维的优化,注意在二维公式中sum的循环是从小到大(从左到右),但是是前一行,转换成一维,需要用到前边的状态,所以要从右向左

dp[j] = dp[j] || dp[j-nums[i]]

dp[0] = true

Time complexity: O(n*target)

Space complexity: O(target)

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

Partition Equal Subset Sum的更多相关文章

  1. LN : leetcode 416 Partition Equal Subset Sum

    lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...

  2. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  3. [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  4. Leetcode 416. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  5. [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  6. 416. Partition Equal Subset Sum

    题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...

  7. Leetcode: Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [Swift]LeetCode416. 分割等和子集 | Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. Leetcode ——Partition Equal Subset Sum

    Question Given a non-empty array containing only positive integers, find if the array can be partiti ...

随机推荐

  1. 安装FP

    一.安装Oracle 11.2 64-bit数据库 1.安装数据库软件并将SEINESCM数据库还原到服务器上, 2.配置监听和TNS信息 二.安装数据库32位客户端(为SSIS配套使用).安装ORA ...

  2. 通过DataTable获得表的主键

    转载http://www.cnblogs.com/hobe/archive/2005/10/07/249940.html 通过DataTable获得表的主键 很多情形下我们需要知道表的主键是什么.在A ...

  3. CSS垂直翻转/水平翻转提高web页面资源重用性

                    /*水平翻转*/ .flipx {     -moz-transform:scaleX(-1);     -webkit-transform:scaleX(-1);   ...

  4. linux 切割文件的命令

    Head -1000 access.2016.log >> 10000_access.log

  5. Dbutils 的JDBC链接

    最近实践周,再次用到了这个Apache的开源工具包就在次对着个方面进行了复习 1.JDBC的基本 操作姿势 2.JDBC的步骤是 1.添加驱动包 Class.forName("oracle. ...

  6. struct和union,enum分析

    空结构体占用的内存多大? struct d { }; int main() { struct d d1; struct d d2; printf("%d,%0x\n",sizeof ...

  7. bs4.BeautifulSoup的基础用法

    导入模块 from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc,"html.parser") 下面看下常见的用法 ...

  8. Java_1简介

    1.Java版本 JavaSE  基础标准版 J2ME     小型版 JavaEE   企业版(主要针对Javaweb程序进行开发) 2.Java特点 开源跨平台 跨平台的原因:Java必须先只能装 ...

  9. 关于AndroidStudio的打包数字签名以及多渠道发布

    AndroidStudio右侧Gradle里边的build(工程下的)是可以生成未签名的debug和release的apk包生成前可以先clean一下工程 app下的build.gradle里边的 l ...

  10. Web API中常用Filter的执行顺序举例讲解

    在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...