Feat : Subset Sum using DP#202
Conversation
| const result = isSubsetSum(set, setLength, sum); | ||
| expect(result).toBe(false); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Why is the indentation off?
| * @param sum - The target sum to be achieved by the subset. | ||
| * @returns True if a subset with the specified sum exists; otherwise, false. | ||
| */ | ||
| export function isSubsetSum(set: number[], setLength: number, sum: number): boolean { |
There was a problem hiding this comment.
setLength is redundant, just use set.length.
There was a problem hiding this comment.
I would like to suggest that the function prototype should be like this:
| export function isSubsetSum(set: number[], setLength: number, sum: number): boolean { | |
| export function isSubsetSum(set: number[], sum: number): boolean { |
| */ | ||
| export function isSubsetSum(set: number[], setLength: number, sum: number): boolean { | ||
|
|
||
| const dp: boolean[][] = new Array(setLength + 1); |
There was a problem hiding this comment.
Please try to give this a proper name, like subsetExists, and add some comment that explains that subsetExists[n][sum] is whether there exists a subset using up to the first n elements in set that sums up to sum.
| } | ||
|
|
||
| for (let i = 0; i <= setLength; i++) { | ||
| dp[i][0] = true; |
There was a problem hiding this comment.
This base case could use a comment (something along the lines of "empty subset sums up to 0")
|
|
||
| for (let i = 1; i <= setLength; i++) { | ||
| for (let j = 1; j <= sum; j++) { | ||
| if (set[i - 1] > j) { |
There was a problem hiding this comment.
This "recursion" formula should be explained.
| const dp: boolean[][] = new Array(setLength + 1); | ||
|
|
||
| for (let i = 0; i <= setLength; i++) { | ||
| dp[i] = new Array(sum + 1).fill(false); | ||
| } |
There was a problem hiding this comment.
What do you think about this suggestion @appgurueu, @Suryac72
| const dp: boolean[][] = new Array(setLength + 1); | |
| for (let i = 0; i <= setLength; i++) { | |
| dp[i] = new Array(sum + 1).fill(false); | |
| } | |
| // subsetExists[n][sum] indicates whether there exists a subset using up to the first n elements in set | |
| // that sums up to sum. | |
| const subsetExists: boolean[][] = Array.from({ length: set.length + 1 }, () => Array(sum + 1).fill(false)); |
| for (let i = 0; i <= setLength; i++) { | ||
| dp[i][0] = true; | ||
| } |
There was a problem hiding this comment.
| for (let i = 0; i <= setLength; i++) { | |
| dp[i][0] = true; | |
| } | |
| // Base case: when sum is 0, there's always an empty subset. | |
| for (let i = 0; i <= set.length; i++) { | |
| subsetExists[i][0] = true; | |
| } |
| for (let i = 1; i <= setLength; i++) { | ||
| for (let j = 1; j <= sum; j++) { | ||
| if (set[i - 1] > j) { | ||
| dp[i][j] = dp[i - 1][j]; | ||
| } else { | ||
| dp[i][j] = dp[i - 1][j] || dp[i - 1][j - set[i - 1]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return dp[setLength][sum]; |
There was a problem hiding this comment.
| for (let i = 1; i <= setLength; i++) { | |
| for (let j = 1; j <= sum; j++) { | |
| if (set[i - 1] > j) { | |
| dp[i][j] = dp[i - 1][j]; | |
| } else { | |
| dp[i][j] = dp[i - 1][j] || dp[i - 1][j - set[i - 1]]; | |
| } | |
| } | |
| } | |
| return dp[setLength][sum]; | |
| for (let currentIndex = 1; currentIndex <= set.length; currentIndex++) { | |
| const currentElement = set[currentIndex - 1]; | |
| for (let currentSum = 1; currentSum <= sum; currentSum++) { | |
| if (currentElement > currentSum) { | |
| // If the current element is greater than the current sum, | |
| // exclude the current element from the subset. | |
| subsetExists[currentIndex][currentSum] = subsetExists[currentIndex - 1][currentSum]; | |
| } else { | |
| // Either include or exclude the current element in the subset. | |
| subsetExists[currentIndex][currentSum] = subsetExists[currentIndex - 1][currentSum] || subsetExists[currentIndex - 1][currentSum - currentElement]; | |
| } | |
| } | |
| } | |
| return subsetExists[set.length][sum]; |
Hi @appgurueu @raklaptudirm please review my PR. I added subset sum problem using DP