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 ...
随机推荐
- Java方法的重载和重写
重载与重写对比: 重载: 权限修饰符(public private 默认):无关 方法名:重载的两个方法的方法名必须相同 形参的个数不同 形参的类型不同 三者至少满足一个 返回值类型: 重载与返回值 ...
- mysql里max_allowed_packet的作用
MySQL根据配置文件会限制Server接受的数据包大小.有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置: 代码如下: show VAR ...
- metasploit framework(一):基本使用
它位于/usr/share/metasploit-framework 进入到modules目录,有六大模块 exploits:系统漏洞利用的流程,对系统漏洞注入一些特定的代码,使其覆盖程序执行寄存器, ...
- 【翻译】View Frustum Culling --2 Geometric Approach – Extracting the Planes
在上一篇中,我们知道了视锥体的形状,并且也确定了我们进行裁剪时的步骤.那我们接下来要走的就是确定视锥体的六个平面: near, far, top, bottom, left and right 2.计 ...
- GreenDao存储自定义类型对象解决方案(转)
最近公司项目选用GreenDao作为Android客户端本地数据库的对象关系映射框架.对于GreenDao虽然以往也有简单用过,但这还是笔者第一次在实际业务中使用.碰到了题目所述的两个问题,虽然在Tu ...
- ajax+js数据模板+后台
.net 后台,ajax+js模板引擎的数据填充,制作无刷新分页 js模板用laytpl 待续...
- python--第二天总结
一.作用域只要变量在内存中存在,则就可以使用.(栈) 二.三元运算result = 值result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为假:result ...
- 二 random模块
1 import random 2 3 print(random.random())#(0,1)----float 大于0且小于1之间的小数 4 5 print(random.randint(1,3) ...
- DBVIS工具 管理数据库链接
- 问题1:鼠标指向导航栏li,但li中a样式未改变
<!--HTML代码--><div class="nav-mid-left"> <ul> <li><a href=" ...