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.

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

  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] 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

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

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

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

  7. Leetcode: Partition Equal Subset Sum

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

  8. [Swift]LeetCode416. 分割等和子集 | Partition Equal Subset Sum

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

  9. Leetcode ——Partition Equal Subset Sum

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

随机推荐

  1. Promise.then

    [Promise.then] 1.If onFulfilled returns a promise, the return value of then will be resolved/rejecte ...

  2. metasploit framework(九):SMB服务扫描

    SMB版本扫描 扫描命名管道,判断SMB服务类型 SMB共享枚举 SMB用户枚举 SID枚举

  3. MongoDB修改默认数据库

    在某些情况下,我们并不想把mongoDB的数据库放在c盘,这时候有两种方法可以切换数据库目录. 1.命令方式 首先创建数据路目录,例如:E:\data\db.然后运行命令 mongod -dbpath ...

  4. 关于MySQL数据库的备份方案

    这里简单总结MySQL的备份分为3种:分为冷备份,逻辑备份,热备份. 1.冷备份: 一般主要用于非核心业务,这类业务一般都是允许业务中断的,冷备份的特点就是数度快,恢复时也最为简单.通常直接复物理文件 ...

  5. 牛客网练习赛12---A and B

    A题传送门:https://www.nowcoder.net/acm/contest/68/A B题传送门:   https://www.nowcoder.net/acm/contest/68/B A ...

  6. mute

    mute - 必应词典 英[mjuːt] n.哑吧:沉默的人:[法律]拒绝答辩的被告人:鸟粪 adj.哑的:缄默无言的:(一时)说不出话的:(猎狗)不叫的 v.排泄:减弱…的声音:柔和…的色调 网络静 ...

  7. monobehaviour生命周期完整版

  8. Cisco无线AP上联口为trunk时无法注册故障处理

    网络环境:WLC5520  AP:1832i-h-k9 客户实施过程中电话告知其将AP和交换机互联的端口类型设置为TRUNK口后,AP无法注册到WLC上,后经过查看配置发现客户用于AP管理的VLAN为 ...

  9. Angular之输入输出属性

    一 父组件 company.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: ...

  10. textarea 自动到右边就换行

    java 到最右边的时候自动换行如实例: textArea.setLineWrap(true);