forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetSumSpaceOptimized.java
More file actions
39 lines (34 loc) · 1.3 KB
/
SubsetSumSpaceOptimized.java
File metadata and controls
39 lines (34 loc) · 1.3 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
package com.thealgorithms.dynamicprogramming;
/**
* Utility class for solving the Subset Sum problem using a space-optimized dynamic programming approach.
*
* <p>This algorithm determines whether any subset of a given array sums up to a specific target value.</p>
*
* <p><b>Time Complexity:</b> O(n * sum)</p>
* <p><b>Space Complexity:</b> O(sum)</p>
*/
public final class SubsetSumSpaceOptimized {
private SubsetSumSpaceOptimized() {
}
/**
* Determines whether there exists a subset of the given array that adds up to the specified sum.
* This method uses a space-optimized dynamic programming approach with a 1D boolean array.
*
* @param nums The array of non-negative integers
* @param targetSum The desired subset sum
* @return {@code true} if such a subset exists, {@code false} otherwise
*/
public static boolean isSubsetSum(int[] nums, int targetSum) {
if (targetSum < 0) {
return false; // Subset sum can't be negative
}
boolean[] dp = new boolean[targetSum + 1];
dp[0] = true; // Empty subset always sums to 0
for (int number : nums) {
for (int j = targetSum; j >= number; j--) {
dp[j] = dp[j] || dp[j - number];
}
}
return dp[targetSum];
}
}