mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 08:17:33 +08:00
style: make SubsetCount
a proper utility (#5153)
This commit is contained in:
@ -74,9 +74,6 @@
|
||||
<Match>
|
||||
<Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING" />
|
||||
</Match>
|
||||
<Match>
|
||||
<Bug pattern="MS_SHOULD_BE_FINAL" />
|
||||
</Match>
|
||||
<Match>
|
||||
<Bug pattern="UWF_UNWRITTEN_FIELD" />
|
||||
</Match>
|
||||
|
@ -6,7 +6,9 @@ package com.thealgorithms.dynamicprogramming;
|
||||
* StackOverflow(https://stackoverflow.com/questions/22891076/count-number-of-subsets-with-sum-equal-to-k)
|
||||
* @author Samrat Podder(https://github.com/samratpodder)
|
||||
*/
|
||||
public class SubsetCount {
|
||||
public final class SubsetCount {
|
||||
private SubsetCount() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamic Programming Implementation.
|
||||
@ -16,7 +18,7 @@ public class SubsetCount {
|
||||
* @param target is the sum of each element of the subset taken together
|
||||
*
|
||||
*/
|
||||
public int getCount(int[] arr, int target) {
|
||||
public static int getCount(int[] arr, int target) {
|
||||
/**
|
||||
* Base Cases - If target becomes zero, we have reached the required sum for the subset
|
||||
* If we reach the end of the array arr then, either if target==arr[end], then we add one to
|
||||
@ -46,7 +48,7 @@ public class SubsetCount {
|
||||
* @param arr is the input array on which subsets are to searched
|
||||
* @param target is the sum of each element of the subset taken together
|
||||
*/
|
||||
public int getCountSO(int[] arr, int target) {
|
||||
public static int getCountSO(int[] arr, int target) {
|
||||
int n = arr.length;
|
||||
int[] prev = new int[target + 1];
|
||||
prev[0] = 1;
|
||||
|
@ -5,27 +5,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SubsetCountTest {
|
||||
public static SubsetCount obj = new SubsetCount();
|
||||
|
||||
@Test
|
||||
void hasMultipleSubset() {
|
||||
int[] arr = new int[] {1, 2, 3, 3};
|
||||
assertEquals(3, obj.getCount(arr, 6));
|
||||
assertEquals(3, SubsetCount.getCount(arr, 6));
|
||||
}
|
||||
@Test
|
||||
void singleElementSubset() {
|
||||
int[] arr = new int[] {1, 1, 1, 1};
|
||||
assertEquals(4, obj.getCount(arr, 1));
|
||||
assertEquals(4, SubsetCount.getCount(arr, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasMultipleSubsetSO() {
|
||||
int[] arr = new int[] {1, 2, 3, 3};
|
||||
assertEquals(3, obj.getCountSO(arr, 6));
|
||||
assertEquals(3, SubsetCount.getCountSO(arr, 6));
|
||||
}
|
||||
@Test
|
||||
void singleSubsetSO() {
|
||||
int[] arr = new int[] {1, 1, 1, 1};
|
||||
assertEquals(1, obj.getCountSO(arr, 4));
|
||||
assertEquals(1, SubsetCount.getCountSO(arr, 4));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user