要求

  • 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等
  • 最多200个数字,每个数字最大为100

示例

  • [1,5,11,5],返回 true
  • [1,2,3,5],返回 false

思路

  • 在n个物品中选出一定物品,填满sum/2的背包
  • 状态:F(n,C)
  • 转移:F(i,c)=F(i-1,c) || F(i-1,c-w(i))
  • 复杂度:O(n*sum/2) = O(n*sum)

实现

递归+记忆化搜索(归纳法)

  • 16-17:是否计算过

 1 class Solution {
2 private:
3 // memo[i][c]表示用索引为[0...i]的元素,是否可以完全填充一个容量为c的背包
4 // -1表示未计算,0表示不可填充,1表示可填充
5 vector<vector<int>> memo;
6
7 // 使用nums[0...index],是否可以完全填充一个容量为sum的背包
8 bool tryPartition(const vector<int> &nums, int index, int sum){
9
10 if( sum == 0 )
11 return true;
12
13 if( sum < 0 || index < 0 )
14 return false;
15
16 if( memo[index][sum] != -1 )
17 return memo[index][sum] == 1;
18
19 memo[index][sum] = ( tryPartition(nums, index-1, sum ) ||
20 tryPartition(nums, index-1, sum-nums[index] ) ) ? 1 : 0;
21 return memo[index][sum] == 1;
22 }
23 public:
24 bool canPartition(vector<int>& nums) {
25 int sum = 0 ;
26 for( int i = 0 ; i < nums.size() ; i ++ ){
27 assert( nums[i] > 0 );
28 sum += nums[i];
29 }
30
31 if( sum%2 != 0 )
32 return false;
33
34 memo = vector<vector<int>>( nums.size(), vector<int>(sum/2+1,-1));
35 return tryPartition( nums, nums.size()-1, sum/2 );
36 }
37 };

动态规划(演绎法)

  • 采用优化方式,memo为一维数组

 1 class Solution {
2
3 public:
4 bool canPartition(vector<int>& nums) {
5 int sum = 0 ;
6 for( int i = 0 ; i < nums.size() ; i ++ ){
7 assert( nums[i] > 0 );
8 sum += nums[i];
9 }
10
11 if( sum%2 != 0 )
12 return false;
13
14 int n = nums.size();
15 int C = sum/2;
16 vector<bool> memo(C+1, false);
17
18 for( int i = 0 ; i <= C ; i ++ )
19 memo[i] = ( nums[0] == i );
20
21 for( int i = 1 ; i < n ; i ++ )
22 for( int j = C ; j >= nums[i] ; j -- )
23 memo[j] = memo[j] || memo[j-nums[i]];
24
25 return memo[C];
26 }
27 };

相关

  • 322 Coin Change
  • 377 Combination Sum IV
  • 474 Ones and Zeros
  • 139 Word Break
  • 494 Target Sum

[刷题] 416 Partition Equal Subset Sum的更多相关文章

  1. LN : leetcode 416 Partition Equal Subset Sum

    lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...

  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. 416. Partition Equal Subset Sum

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

  5. [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集

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

  6. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

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

  7. LC 416. Partition Equal Subset Sum

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

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

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

  9. 【leetcode】416. Partition Equal Subset Sum

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

随机推荐

  1. APP | edxposed框架+trustmealredy模块抓包小程序

    出品|MS08067实验室(www.ms08067.com) 本文作者:ketchup(Ms08067实验室 SRSP TEAM小组成员) 一.下载edxposed框架,由于安卓5.0版本以下的不支持 ...

  2. SQL语句通过身份证号计算年龄

    SQL语句通过身份证号计算年龄 1.截取身份证号上的出生日期 身份证一般为18位数和15位数 18位数身份证的第7-10位数是出生年份,第11-14位数是出生月日,所以18位身份证的年龄计算如下 su ...

  3. Mybatis-plus 下

    Mybatis-plus 下 查询操作 1.查询单个用户 @Test public void testSelectById(){ User user = userMapper.selectById(1 ...

  4. 因为这几个TypeScript代码的坏习惯,同事被罚了500块

    作者:Daniel Bartholomae 翻译:疯狂的技术宅 原文链接:https://startup-cto.net/10-bad-typescript-habits-to-break-this- ...

  5. 如果你的application.properties中还存在明文密码----加密Spring Boot中的application.properties

    1 概述 什么?都2020年了还在Spring Boot的配置文件中写明文密码? 虽然是小项目,明文也没人看. 明文简单快捷方便啊!!! 你看直接用户名root密码123456多么简单!!! ... ...

  6. Day14_84_通过反射机制修改和获取class里的属性值

    通过反射机制修改和获取class里的属性值 * 属性对象.set(Object,属性值) 给Object对象中的某个属性赋值(属性对象) * 属性对象.get(Object); 获取Object对象中 ...

  7. H5小技巧之——巧用<a>标签锚链接(#锚点链接 #页面特定位置 #DOM定位 #hash路由中使用锚链接)

    #作者:矩阵鱼--代码中游泳的咸鱼 前端开发中,常遇到定位到页面某特定位置的需求,JavaScript提供的el.scrollIntoView() 和 el.scrollIntoViewIfNeede ...

  8. Sql server注入一些tips

    sql server环境测试: 几个特性: 1.sql server兼容性可以说是最差的. 举例: select x from y where id=1 字符串查询 select x from y w ...

  9. 粗浅聊聊Python装饰器

    浅析装饰器 通常情况下,给一个对象添加新功能有三种方式: 直接给对象所属的类添加方法: 使用组合:(在新类中创建原有类的对象,重复利用已有类的功能) 使用继承:(可以使用现有类的,无需重复编写原有类进 ...

  10. hdu4965 巧用矩阵乘法结合律

    题意:      给两个矩阵,n*m的矩阵A,和m*n的矩阵B, 求(A*B)^(n*n)其中 m<=6,n<=1000. 思路:       一开始直接模拟,写了个矩阵快速幂,超时了,因 ...