From d73e66f7cc5700022ab12d8cb4fb127cc594fea2 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 13 Jul 2025 13:32:20 +0200 Subject: [PATCH] refactor: improving readability DecimalToAnyUsingStack --- .../SubsetSumSpaceOptimized.java | 48 ++++++++++--------- .../stacks/DecimalToAnyUsingStack.java | 29 +++++++---- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java index 946da46cb292..5c0167977ae4 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java @@ -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. + * + *

This algorithm determines whether any subset of a given array sums up to a specific target value.

+ * + *

Time Complexity: O(n * sum)

+ *

Space Complexity: O(sum)

+ */ 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]; } } diff --git a/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java b/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java index ff6402c92695..2852454fd096 100644 --- a/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java +++ b/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java @@ -2,17 +2,29 @@ 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. + * + *

This class uses a stack-based approach to reverse the digits obtained from + * successive divisions by the target radix. + * + *

This class cannot be instantiated.

+ */ 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 number is negative or radix 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 static String convert(int number, int radix) { return "0"; } - char[] tables = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - - Stack bits = new Stack<>(); + Stack 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(); } }