-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionEqualSubsetSum.java
More file actions
57 lines (55 loc) · 1.25 KB
/
PartitionEqualSubsetSum.java
File metadata and controls
57 lines (55 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package medium;
/**
* ClassName: PartitionEqualSubsetSum.java
* Author: chenyiAlone
* Create Time: 2019/8/11 23:01
* Description: No.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.
*
*/
public class PartitionEqualSubsetSum {
public boolean canPartition(int[] nums) {
int sum = 0;
for (int n : nums) {
sum += n;
}
if (sum % 2 != 0) return false;
sum /= 2;
boolean[] f = new boolean[sum + 1];
f[0] = true;
for (int i : nums) {
for (int j = sum; j >= 0; j--) {
if (f[j] && i + j <= sum)
f[j + i] = true;
}
}
return f[sum];
}
}