The code provides here fails for the test case [1,2,5] expected is false but the code returns true.
Here is the code for Top-down DP with memoization: The code provided at this line
The code provides here fails for the test case [1,2,5] expected is false but the code returns true.
Here is the code for Top-down DP with memoization: The code provided at this line
boolean canPartition(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
if (sum % 2 == 1) {
return false;
}
sum = sum >>> 1;
boolean dp[] = new boolean[sum + 1];
dp[0] = true;
for (int num : nums) {
for(int s = sum; s >= 0; s--) {
if (s >= num) {
dp[s] = dp[s - num];
}
}
}
return dp[sum];
}