Skip to content

refactor: improving readability DecimalToAnyUsingStack #6377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <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) {
Expand All @@ -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<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();
}
}
Loading