Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1554ba5

Browse files
authored
test: add tests forNumberOfSubsetEqualToGivenSum (#1661)
1 parent3623e42 commit1554ba5

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

‎Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
/*
2-
Given an array ofnon-negative integers and a value sum,
2+
Given an array ofpositive integers and a value sum,
33
determine the total number of the subset with sum
44
equal to the given sum.
55
*/
66
/*
77
Given solution is O(n*sum) Time complexity and O(sum) Space complexity
88
*/
99
functionNumberOfSubsetSum(array,sum){
10+
if(sum<0){
11+
thrownewError('The sum must be non-negative.')
12+
}
13+
14+
if(!array.every((num)=>num>0)){
15+
thrownewError('All of the inputs of the array must be positive.')
16+
}
1017
constdp=[]// create an dp array where dp[i] denote number of subset with sum equal to i
1118
for(leti=1;i<=sum;i++){
1219
dp[i]=0
@@ -23,10 +30,4 @@ function NumberOfSubsetSum(array, sum) {
2330
returndp[sum]
2431
}
2532

26-
// example
27-
28-
// const array = [1, 1, 2, 2, 3, 1, 1]
29-
// const sum = 4
30-
// const result = NumberOfSubsetSum(array, sum)
31-
3233
export{NumberOfSubsetSum}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import{NumberOfSubsetSum}from'../NumberOfSubsetEqualToGivenSum'
2+
3+
describe('Testing NumberOfSubsetSum',()=>{
4+
it.each([
5+
[[],0,1],
6+
[[],1,0],
7+
[[1],2,0],
8+
[[1,2,3,4,5],0,1],
9+
[[1,1,1,1,1],5,1],
10+
[[1,1,1,1,1],4,5],
11+
[[1,2,3,3],6,3],
12+
[[10,20,30,1],31,2],
13+
[[1,1,2,2,3,1,1],4,18]
14+
])('check with %j and %i',(arr,sum,expected)=>{
15+
expect(NumberOfSubsetSum(arr,sum)).toBe(expected)
16+
})
17+
18+
it.each([
19+
[[1,2],-1],
20+
[[0,2],2],
21+
[[1,-1],0]
22+
])('throws for %j and %i',(arr,sum)=>{
23+
expect(()=>NumberOfSubsetSum(arr,sum)).toThrowError()
24+
})
25+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp