refactor: improving readability DecimalToAnyUsingStack (#6377)

refactor: improving readability DecimalToAnyUsingStack

Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
This commit is contained in:
Oleksandr Klymenko
2025-07-15 08:26:49 +03:00
committed by GitHub
parent 95116dbee4
commit 7e37d94c53
2 changed files with 46 additions and 31 deletions

View File

@@ -1,35 +1,39 @@
package com.thealgorithms.dynamicprogramming;
/*
The Sum of Subset problem determines whether a subset of elements from a
given array sums up to a specific target value.
*/
/**
* 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() {
}
/**
* This method checks whether the subset of an array
* contains a given sum or not. This is an space
* optimized solution using 1D boolean array
* Time Complexity: O(n * sum), Space complexity: O(sum)
* 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 arr An array containing integers
* @param sum The target sum of the subset
* @return True or False
* @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[] arr, int sum) {
int n = arr.length;
// Declare the boolean array with size sum + 1
boolean[] dp = new boolean[sum + 1];
public static boolean isSubsetSum(int[] nums, int targetSum) {
if (targetSum < 0) {
return false; // Subset sum can't be negative
}
// Initialize the first element as true
dp[0] = true;
boolean[] dp = new boolean[targetSum + 1];
dp[0] = true; // Empty subset always sums to 0
// Find the subset sum using 1D array
for (int i = 0; i < n; i++) {
for (int j = sum; j >= arr[i]; j--) {
dp[j] = dp[j] || dp[j - arr[i]];
for (int number : nums) {
for (int j = targetSum; j >= number; j--) {
dp[j] = dp[j] || dp[j - number];
}
}
return dp[sum];
return dp[targetSum];
}
}

View File

@@ -2,17 +2,29 @@ package com.thealgorithms.stacks;
import java.util.Stack;
/**
* Utility class for converting a non-negative decimal (base-10) integer
* to its representation in another radix (base) between 2 and 16, inclusive.
*
* <p>This class uses a stack-based approach to reverse the digits obtained from
* successive divisions by the target radix.
*
* <p>This class cannot be instantiated.</p>
*/
public final class DecimalToAnyUsingStack {
private DecimalToAnyUsingStack() {
}
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* Convert a decimal number to another radix.
*
* @param number the number to be converted
* @param radix the radix
* @return the number represented in the new radix as a String
* @throws IllegalArgumentException if <tt>number</tt> is negative or <tt>radix</tt> is not between 2 and 16 inclusive
* @throws IllegalArgumentException if number is negative or radix is not between 2 and 16 inclusive
*/
public static String convert(int number, int radix) {
if (number < 0) {
@@ -26,18 +38,17 @@ public final class DecimalToAnyUsingStack {
return "0";
}
char[] tables = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
Stack<Character> bits = new Stack<>();
Stack<Character> digitStack = new Stack<>();
while (number > 0) {
bits.push(tables[number % radix]);
number = number / radix;
digitStack.push(DIGITS[number % radix]);
number /= radix;
}
StringBuilder result = new StringBuilder();
while (!bits.isEmpty()) {
result.append(bits.pop());
StringBuilder result = new StringBuilder(digitStack.size());
while (!digitStack.isEmpty()) {
result.append(digitStack.pop());
}
return result.toString();
}
}