Skip to content

Commit 7e37d94

Browse files
refactor: improving readability DecimalToAnyUsingStack (TheAlgorithms#6377)
refactor: improving readability DecimalToAnyUsingStack Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
1 parent 95116db commit 7e37d94

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
package com.thealgorithms.dynamicprogramming;
2-
/*
3-
The Sum of Subset problem determines whether a subset of elements from a
4-
given array sums up to a specific target value.
5-
*/
2+
3+
/**
4+
* Utility class for solving the Subset Sum problem using a space-optimized dynamic programming approach.
5+
*
6+
* <p>This algorithm determines whether any subset of a given array sums up to a specific target value.</p>
7+
*
8+
* <p><b>Time Complexity:</b> O(n * sum)</p>
9+
* <p><b>Space Complexity:</b> O(sum)</p>
10+
*/
611
public final class SubsetSumSpaceOptimized {
712
private SubsetSumSpaceOptimized() {
813
}
14+
915
/**
10-
* This method checks whether the subset of an array
11-
* contains a given sum or not. This is an space
12-
* optimized solution using 1D boolean array
13-
* Time Complexity: O(n * sum), Space complexity: O(sum)
16+
* Determines whether there exists a subset of the given array that adds up to the specified sum.
17+
* This method uses a space-optimized dynamic programming approach with a 1D boolean array.
1418
*
15-
* @param arr An array containing integers
16-
* @param sum The target sum of the subset
17-
* @return True or False
19+
* @param nums The array of non-negative integers
20+
* @param targetSum The desired subset sum
21+
* @return {@code true} if such a subset exists, {@code false} otherwise
1822
*/
19-
public static boolean isSubsetSum(int[] arr, int sum) {
20-
int n = arr.length;
21-
// Declare the boolean array with size sum + 1
22-
boolean[] dp = new boolean[sum + 1];
23+
public static boolean isSubsetSum(int[] nums, int targetSum) {
24+
if (targetSum < 0) {
25+
return false; // Subset sum can't be negative
26+
}
2327

24-
// Initialize the first element as true
25-
dp[0] = true;
28+
boolean[] dp = new boolean[targetSum + 1];
29+
dp[0] = true; // Empty subset always sums to 0
2630

27-
// Find the subset sum using 1D array
28-
for (int i = 0; i < n; i++) {
29-
for (int j = sum; j >= arr[i]; j--) {
30-
dp[j] = dp[j] || dp[j - arr[i]];
31+
for (int number : nums) {
32+
for (int j = targetSum; j >= number; j--) {
33+
dp[j] = dp[j] || dp[j - number];
3134
}
3235
}
33-
return dp[sum];
36+
37+
return dp[targetSum];
3438
}
3539
}

src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22

33
import java.util.Stack;
44

5+
/**
6+
* Utility class for converting a non-negative decimal (base-10) integer
7+
* to its representation in another radix (base) between 2 and 16, inclusive.
8+
*
9+
* <p>This class uses a stack-based approach to reverse the digits obtained from
10+
* successive divisions by the target radix.
11+
*
12+
* <p>This class cannot be instantiated.</p>
13+
*/
514
public final class DecimalToAnyUsingStack {
15+
616
private DecimalToAnyUsingStack() {
717
}
818

19+
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
20+
921
/**
1022
* Convert a decimal number to another radix.
1123
*
1224
* @param number the number to be converted
1325
* @param radix the radix
1426
* @return the number represented in the new radix as a String
15-
* @throws IllegalArgumentException if <tt>number</tt> is negative or <tt>radix</tt> is not between 2 and 16 inclusive
27+
* @throws IllegalArgumentException if number is negative or radix is not between 2 and 16 inclusive
1628
*/
1729
public static String convert(int number, int radix) {
1830
if (number < 0) {
@@ -26,18 +38,17 @@ public static String convert(int number, int radix) {
2638
return "0";
2739
}
2840

29-
char[] tables = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
30-
31-
Stack<Character> bits = new Stack<>();
41+
Stack<Character> digitStack = new Stack<>();
3242
while (number > 0) {
33-
bits.push(tables[number % radix]);
34-
number = number / radix;
43+
digitStack.push(DIGITS[number % radix]);
44+
number /= radix;
3545
}
3646

37-
StringBuilder result = new StringBuilder();
38-
while (!bits.isEmpty()) {
39-
result.append(bits.pop());
47+
StringBuilder result = new StringBuilder(digitStack.size());
48+
while (!digitStack.isEmpty()) {
49+
result.append(digitStack.pop());
4050
}
51+
4152
return result.toString();
4253
}
4354
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy