forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0416-partition-equal-subset-sum.java
47 lines (43 loc) · 1.37 KB
/
0416-partition-equal-subset-sum.java
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
class Solution {
// TC = O(n*sum), SC = O(n*sum)
public boolean canPartition(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
if (sum % 2 != 0) return false;
int target = sum / 2;
boolean[][] dp = new boolean[nums.length + 1][target + 1];
int n = nums.length;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= target; j++) {
if (i == 0 || j == 0) {
if (i == 0) dp[i][j] = false; else if (j == 0) dp[i][j] =
true;
} else if (j >= nums[i - 1]) {
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i - 1]];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[n][target];
}
// TC = O(n*sum), SC = O(sum)
public boolean canPartition(int[] nums) {
int sum = Arrays.stream(nums).sum();
if (sum % 2 != 0) return false;
int target = sum / 2;
boolean[] dp = new boolean[target];
dp[0] = true;
for (int no : nums) {
for (int i = target; i >= no; i--) {
if (dp[i - no] == true) {
if (i == target) return true;
dp[i] = true;
}
}
}
return false;
}
}