Leetcode: 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.
Backpack problem: dp[i][j] means if the first i elements can sum up to value j
dp[i][j] = dp[i-1][j] || (j>=nums[i-1] && dp[i-1][j-nums[i-1]])
the result is if the half sum of the array can be summed up by elements in the array
public class Solution {
public boolean canPartition(int[] nums) {
if (nums.length == 0) return true;
int volume = 0;
for (int num : nums) {
volume += num;
}
if (volume % 2 == 1) return false;
volume /= 2;
boolean[] dp = new boolean[volume+1];
dp[0] = true;
for (int i=1; i<=nums.length; i++) {
for (int j=volume; j>=0; j--) {
dp[j] = dp[j] || (j>=nums[i-1] && dp[j-nums[i-1]]);
}
}
return dp[volume];
}
}
Leetcode: Partition Equal Subset Sum的更多相关文章
- [LeetCode] 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 ...
- LeetCode—— Partition Equal Subset Sum
Question Given a non-empty array containing only positive integers, find if the array can be partiti ...
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- 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 ...
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
随机推荐
- 【BZOJ1503】 [NOI2004]郁闷的出纳员 splay
splay模板题,都快把我做忧郁了. 由于自己调两个坑点. 1.删除时及时updata 2.Kth 考虑k满足该点的条件即r->ch[1]->size+1<=k && ...
- weblogic 11g 配置db2数据源
配置db2数据源可以直接在包里面配置,不需要专门在服务器上配置数据源. 在11g版本前要配置db2数据源是需要增加包,后续的版本处理了这个问题. 1. 将C:\Program Files\SQLLIB ...
- Makefile简易模板
MAKE = g++ -g #MAKE = gcc -g FILES = tf all : $(FILES) #DYSRC = target.c #DYTGT = $(DYSRC:.c=.o) %.o ...
- C#字符串处理(String与StringBuilder)
首先介绍一下常用的几个功能:Compare(str1,str2)——比较两个字符串str1,str2的大小,如果大于返回正数,等于返回0,小于返回负数!IndexOf——定位字符串中第一次出现某个给定 ...
- 一个资深iOS开发者对于React Native的看法
一个资深iOS开发者对于React Native的看法 当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javasc ...
- win7/IE8无法加载QCbin的插件
pian A: 1.控制面板->系统和安全->更改用户账户控制设置->安全等级调至最低->关机重启 2.打开IE浏览器->工具->Internet选项->高级 ...
- html使用心得
(1)<textarea style= "word-break:break-all" rows="5" cols="20"> 在 ...
- map的用法
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数: map<stri ...
- 使用Fiddler截断更改Request数据
0x01 Fiddler介绍 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据.(百度百科) 0x ...
- bootstrap input框清空
<!DOCTYPE HTML> <html> <head> <link href="http://netdna.bootstrapcdn.com/t ...