LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum
416 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.
DP Accepted
这其实是一个01背包问题,对于每一个数,我们可以选择放入和不放入,使dp[i][j]代表数组中前i个进行选择能否可以累计得到j。dp[0][0]为1,其余情况dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]],因为要么不选择,那么就为dp[i-1][j],要么选择,那么就为dp[i-1][j-nums[i]]。
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum & 1) return false;
int target = sum >> 1;
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (auto num : nums)
for (int i = target; i >= num; i--)
dp[i] = dp[i] || dp[i-num];
return dp[target];
}
};
LN : leetcode 416 Partition Equal Subset Sum的更多相关文章
- [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 ...
- 【leetcode】416. Partition Equal Subset Sum
题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...
- 416. Partition Equal Subset Sum
题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...
- 416 Partition Equal Subset Sum 分割相同子集和
详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...
- LC 416. Partition Equal Subset Sum
题目 Given a non-empty array containing only positive integers, find if the array can be partitioned i ...
- [刷题] 416 Partition Equal Subset Sum
要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...
随机推荐
- 对象数组、集合、链表(java基础知识十五)
1.对象数组的概述和使用 * 需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息. Student[] arr = new Student[5]; //存储学生 ...
- javascript flash 弹框
1. [代码]FlashBox // JavaScript Documentfunction FlashBox(src,width,height){var docbody = document ...
- UVA-11078(水题)
题意: 给一个序列,找两个整数a[i],a[j]使得a[i]-a[j]最大; 思路: 从前往后扫一遍;水题; AC代码: #include <bits/stdc++.h> /* #incl ...
- [Selenium] Selenium find Element Examples
---> 1. By.id 以百度主页为例 <span classs = "bg s_ipt_wr"> <input type = "text& ...
- 【USACO】 Balanced Photo
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...
- [USACO11NOV]二进制数独Binary Sudoku
传送门 这道题是很好的一道IDA*练习题. 首先我们先确定搜索的框架,我们要求的是用最少的修改次数使得所有的行,列,宫之内都有偶数个1,最直观的想法显然是先预处理出有奇数个1的行,列,宫,之后枚举每一 ...
- Excel: 使用Countif函数来去掉Excel中重复的数据
如果使用Ruby脚本,uniq函数就能搞定一切.不过我们现在还是讨论怎么用excel的Countif函数吧. 假设Excel中有一列数据:
- http查看工具
View HTTP Request and Response Header Web-Sniffer Desktop App Please check our new free Web-Sniffer ...
- 使用lsyncd配置数据库备份多异地同步
lsyncd配置文件 settings { logfile = "/var/log/lsyncd.log", --日志路径 status = "/var/log/lsyn ...
- find 是区分大小写的。对于不区分大小写的写法(转载)
转自:http://justwinit.cn/post/3633/ 默认情况下,find 是区分大小写的.对于不区分大小写的 find,将 -iname 测试替换为 -name 测试. find do ...