-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCodeQ416.java
More file actions
76 lines (66 loc) · 2.21 KB
/
leetCodeQ416.java
File metadata and controls
76 lines (66 loc) · 2.21 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package DynamicProgramming;
public class leetCodeQ416 {
// Recursion + Memoization T.C & A.S ==> O (n * t) .
public static boolean subset(int[] arr, int target, int idx, int[][] dp) {
if (idx == arr.length) {
if (target == 0)
return true;
else
return false;
}
if (dp[idx][target] != -1)
return (dp[idx][target] == 1);
boolean ans = false;
boolean skip = subset(arr, target, idx + 1, dp);
if (target - arr[idx] < 0)
ans = skip; // Only Valid for +ve No.
else {
boolean take = subset(arr, target - arr[idx], idx + 1, dp);
ans = skip || take;
}
dp[idx][target] = (ans) ? 1 : 0;
return ans;
}
// Tabulation
public static boolean canPartition(int[] nums) {
int sum = 0;
for (int ele : nums)
sum += ele;
if (sum % 2 == 1)
return false;
int n = nums.length;
int target = sum / 2;
int[][] dp = new int[n][target + 1];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
boolean ans = false;
boolean skip = (i > 0) ? (dp[i - 1][j] == 1) : (j == 0);
if (j - nums[i] < 0)
ans = skip; // Only Valid for +ve No.
else {
boolean take = (i > 0) ? (dp[i - 1][j - nums[i]] == 1) : (j == 0);
ans = skip || take;
}
dp[i][j] = (ans) ? 1 : 0;
}
}
return (dp[n - 1][target] == 1);
}
public static void main(String[] args) {
int[] nums = { 1, 5, 11, 5 } ;
int sum = 0;
for (int ele : nums)
sum += ele;
int[][] dp = new int[nums.length][sum + 1];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1;
}
}
if (sum % 2 == 1)
System.out.println(false);
else
System.out.println(subset(nums, sum/2, 0, dp));
System.out.println(canPartition(nums));
}
}