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:

  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.

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的更多相关文章

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

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

  2. Leetcode 416. 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 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

  5. 【leetcode】416. Partition Equal Subset Sum

    题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...

  6. 416. Partition Equal Subset Sum

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

  7. 416 Partition Equal Subset Sum 分割相同子集和

    详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...

  8. LC 416. Partition Equal Subset Sum

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

  9. [刷题] 416 Partition Equal Subset Sum

    要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...

随机推荐

  1. java反射机制与动态加载类

    什么是java反射机制? 1.当程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言.我们认为java并不是动态语言,但是它却有一个非常突出的动态相关机制,俗称:反射. IT行业里这么说,没有 ...

  2. Ubuntu安装mycli,让mysql命令行可以自动提示

    安装mycli 1.确保有安装python 2.确保有安装pip 3.进入su模式,以管理员身份安装 4.安装 pip install -U mycli 5.登录 mycli -u root 很好很强 ...

  3. JavaScript函数调用规则

    1. [代码][JavaScript]代码     JavaScript函数调用规则一 (1)全局函数调用:function makeArray( arg1, arg2 ){    return [t ...

  4. [Silverlight 2.0 控制物体绕圆弧运行(C#初探篇)]

    我自己写的第一个 Silverlight 2.0 程序    [Silverlight 2.0 控制物体绕圆弧运行(C#初探篇)]            程序运行时:小地球将绕着圆形轨迹做圆周运动. ...

  5. skynet实践(8)-接入websocket

    我从开源项目(https://github.com/lipp/lua-websockets,这里我们简称LWS)中抽出了websocket的部分处理,步骤如下: 1)首先是解决LWS的几个依赖问题.L ...

  6. php连接数据库步骤

    第一步:连接数据库 $link=@mysql_connect('localhost','root','root') or die('数据库连接失败!'); echo '连接成功!'; 这里数据库连接函 ...

  7. SPOJ:Bits. Exponents and Gcd(组合数+GCD)

    Rastas's has been given a number n. Being weak at mathematics, she has to consider all the numbers f ...

  8. angularJS 的双向数据绑定

    input 里面的vale="变量名";加上ng-model="变量名";控制器的变量名会根据视图层的数据改变而改变,而渲染内容也会根据控制器里面的变量改变而改 ...

  9. "Activity" 总结

    1.什么是Activity? 1.四大组件之一 2.通常一个界面对应一个activity 3.是Context的子类 4.同时实现window.callback和keyevent.callback回调 ...

  10. UI:UICollectionView

    #import "ViewController.h" #import "HeaderView.h" #import "FooterView.h&quo ...