style: make SubsetCount a proper utility (#5153)

This commit is contained in:
Piotr Idzik
2024-05-11 16:36:17 +02:00
committed by GitHub
parent cb401fed69
commit cf6c87c35c
3 changed files with 9 additions and 12 deletions

View File

@ -74,9 +74,6 @@
<Match> <Match>
<Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING" /> <Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING" />
</Match> </Match>
<Match>
<Bug pattern="MS_SHOULD_BE_FINAL" />
</Match>
<Match> <Match>
<Bug pattern="UWF_UNWRITTEN_FIELD" /> <Bug pattern="UWF_UNWRITTEN_FIELD" />
</Match> </Match>

View File

@ -6,7 +6,9 @@ package com.thealgorithms.dynamicprogramming;
* StackOverflow(https://stackoverflow.com/questions/22891076/count-number-of-subsets-with-sum-equal-to-k) * StackOverflow(https://stackoverflow.com/questions/22891076/count-number-of-subsets-with-sum-equal-to-k)
* @author Samrat Podder(https://github.com/samratpodder) * @author Samrat Podder(https://github.com/samratpodder)
*/ */
public class SubsetCount { public final class SubsetCount {
private SubsetCount() {
}
/** /**
* Dynamic Programming Implementation. * Dynamic Programming Implementation.
@ -16,7 +18,7 @@ public class SubsetCount {
* @param target is the sum of each element of the subset taken together * @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 * 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 * 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 arr is the input array on which subsets are to searched
* @param target is the sum of each element of the subset taken together * @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 n = arr.length;
int[] prev = new int[target + 1]; int[] prev = new int[target + 1];
prev[0] = 1; prev[0] = 1;

View File

@ -5,27 +5,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class SubsetCountTest { public class SubsetCountTest {
public static SubsetCount obj = new SubsetCount();
@Test @Test
void hasMultipleSubset() { void hasMultipleSubset() {
int[] arr = new int[] {1, 2, 3, 3}; int[] arr = new int[] {1, 2, 3, 3};
assertEquals(3, obj.getCount(arr, 6)); assertEquals(3, SubsetCount.getCount(arr, 6));
} }
@Test @Test
void singleElementSubset() { void singleElementSubset() {
int[] arr = new int[] {1, 1, 1, 1}; int[] arr = new int[] {1, 1, 1, 1};
assertEquals(4, obj.getCount(arr, 1)); assertEquals(4, SubsetCount.getCount(arr, 1));
} }
@Test @Test
void hasMultipleSubsetSO() { void hasMultipleSubsetSO() {
int[] arr = new int[] {1, 2, 3, 3}; int[] arr = new int[] {1, 2, 3, 3};
assertEquals(3, obj.getCountSO(arr, 6)); assertEquals(3, SubsetCount.getCountSO(arr, 6));
} }
@Test @Test
void singleSubsetSO() { void singleSubsetSO() {
int[] arr = new int[] {1, 1, 1, 1}; int[] arr = new int[] {1, 1, 1, 1};
assertEquals(1, obj.getCountSO(arr, 4)); assertEquals(1, SubsetCount.getCountSO(arr, 4));
} }
} }