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:
- Each of the array element will not exceed 100.
- 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的更多相关文章
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode 416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- 416. Partition Equal Subset Sum
题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...
- Leetcode: Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [Swift]LeetCode416. 分割等和子集 | Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode ——Partition Equal Subset Sum
Question Given a non-empty array containing only positive integers, find if the array can be partiti ...
随机推荐
- onMouseOver&onMouseOut vs onMouseEnter&onMouseLeave
[onMouseOver&onMouseOut vs onMouseEnter&onMouseLeave] 1.onmouseleave.onmouseenter,鼠标进入到指定元素区 ...
- python3替换文件的内容
目标:替换文件中的字符串内容 方法1:使用fileinput包 import fileinput for line in fileinput.input(“要修改的文件名", inp ...
- js常用返回网页顶部几种方法
一.使用锚标记 此方法最简单,只需在body下放个隐藏的锚点标记,内容如下: 代码如下 复制代码 <a name="top" id="top">& ...
- php苹果内购订单验证
/** * 21000 App Store不能读取你提供的JSON对象 * 21002 receipt-data域的数据有问题 * 21003 receipt无法通过验证 * 21004 提供的sha ...
- Django2.1在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
解决办法: a=models.ForeignKey('BookInfo',on_delete=models.CASCADE,) 即在外键值的后面加上 on_delete=models.CASCADE ...
- Openstack 集群,及常用服务的 高可用 haproxy配置
一.介绍 配置文件位置(yum 安装):/etc/haproxy/haproxy.cfg 全局配置 #------------------------------------------------- ...
- AI图谱
- 微信小程序开发——模板中加载html代码
最新方法可以使用微信小程序提供的 rich-text (富文本)组件直接写解析html,详见 rich-text: <rich-text class='f13 c_9' nodes=" ...
- 明明白白你的Linux服务器——日志篇
日志对于安全来说,非常重要,它记录了系统每天发生的各种各样的事情,你可以通过他来检查错误发生的原因,或者受到攻击时攻击者留下的痕迹.日志主要的功能有:审计和监测.他还可以实时的监测系统状态,监测和追踪 ...
- 用Jenkins自动化搭建测试环境-前奏
用Jenkins自动化搭建测试环境-前奏 1.安装 参考及启动:https://www.cnblogs.com/Eric15/articles/9828062.html 2.插件 新手一般按推荐安装即 ...