-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_416.java
More file actions
38 lines (37 loc) · 1.1 KB
/
a_416.java
File metadata and controls
38 lines (37 loc) · 1.1 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
package dynamicProgramming;
/**
* 划分数组为和相等的两部分
* https://www.cnblogs.com/shinning/p/6027743.html
* https://www.cnblogs.com/fengziwei/p/7755865.html
* https://blog.csdn.net/mebiuw/article/details/52765840
*/
public class a_416 {
public boolean canPartition(int[] nums) {
int sum = computeArraySum(nums);
if (sum % 2 != 0) return false;
sum /= 2;
/*int[] dp = new int[sum+1];
for (int i = 0; i < nums.length; i++) {
for (int j = sum; j >= nums[i]; j--) {
dp[j] =Math.max(dp[j], dp[j-nums[i]] + nums[i]);
}
}
return dp[sum] == sum;*/
boolean[] dp = new boolean[sum+1];
dp[0] = true;
for (int i = 0; i < nums.length; i++) {
for (int j = sum; j >= nums[i]; j--) {
dp[j] = dp[j] || dp[j-nums[i]];
}
if (dp[sum]) return true;
}
return false;
}
private int computeArraySum(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
return sum;
}
}