Skip to content

Commit 3a85699

Browse files
Merge branch 'master' into refactor/DisjointSetUnion
2 parents 574aa62 + 7e37d94 commit 3a85699

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
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/matrix/MedianOfMatrix.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ private MedianOfMatrix() {
1414
}
1515

1616
public static int median(Iterable<List<Integer>> matrix) {
17-
// Flatten the matrix into a 1D list
18-
List<Integer> linear = new ArrayList<>();
17+
List<Integer> flattened = new ArrayList<>();
18+
1919
for (List<Integer> row : matrix) {
20-
linear.addAll(row);
20+
if (row != null) {
21+
flattened.addAll(row);
22+
}
2123
}
2224

23-
// Sort the 1D list
24-
Collections.sort(linear);
25-
26-
// Calculate the middle index
27-
int mid = (0 + linear.size() - 1) / 2;
25+
if (flattened.isEmpty()) {
26+
throw new IllegalArgumentException("Matrix must contain at least one element.");
27+
}
2828

29-
// Return the median
30-
return linear.get(mid);
29+
Collections.sort(flattened);
30+
return flattened.get((flattened.size() - 1) / 2);
3131
}
3232
}

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
}

src/test/java/com/thealgorithms/matrix/MedianOfMatrixTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.thealgorithms.matrix;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.ArrayList;
67
import java.util.Arrays;
8+
import java.util.Collections;
79
import java.util.List;
810
import org.junit.jupiter.api.Test;
911

@@ -31,4 +33,18 @@ public void testMedianWithEvenNumberOfElements() {
3133

3234
assertEquals(2, result);
3335
}
36+
37+
@Test
38+
public void testMedianSingleElement() {
39+
List<List<Integer>> matrix = new ArrayList<>();
40+
matrix.add(List.of(1));
41+
42+
assertEquals(1, MedianOfMatrix.median(matrix));
43+
}
44+
45+
@Test
46+
void testEmptyMatrixThrowsException() {
47+
Iterable<List<Integer>> emptyMatrix = Collections.emptyList();
48+
assertThrows(IllegalArgumentException.class, () -> MedianOfMatrix.median(emptyMatrix), "Expected median() to throw, but it didn't");
49+
}
3450
}

0 commit comments

Comments
 (0)