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. Centos7安装出现的问题:找不到安装源或者检查软件配置出错

    安装启动时到以下界面 此时,按一下Tab键,将会出现在屏幕下方出现这一串文字 vmlinuz initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x2 ...

  2. 用户维护 UI 检验周期更新逻辑

    在SAP系统中建立UI,供用户维护物料组对应的检验周期,FP按照物料组对应的物料编码取UI维护的检验周期进FP系统规划. --物料组对应检验周期维护表(新UI)add by landor on 201 ...

  3. java面试技巧

    简历 1.HR看简历,都是看技术关键词.可以多看招聘要求,简历上要多写些关键词.比如io,集合,多线程,并发,spring,mysql,分布式等等. 2.可以准备多份简历,根据不同的jd发送不同的简历 ...

  4. 彻底弄懂css中单位px和em,rem的区别

    PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...

  5. 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. 【转】web.xml配置项详解

    史上最全web.xml配置文件元素详解   一.web.xml配置文件常用元素及其意义预览 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <d ...

  7. Codeforces Beta Round #22 (Div. 2 Only)

    Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 #include<bits/stdc+ ...

  8. React学习札记一

    I’m in a hurry! 我在赶时间! It’s her field. 这是她的本行. It’s up to you. 由你决定. You owe me one.你欠我一个人情. 1.React ...

  9. nodejs异步读数据库

    以下代码不完美,但讲明了使用方法. 回调: function selectUser(callback) { var sql = "SELECT * FROM user"; conn ...

  10. 【go语言实现服务器接收http请求以及出现泄漏时的解决方案】

    一.关于基础的程序的实现 刚开始的时候程序是这样实现的: // Hello package main import ( "database/sql" "fmt" ...