
g0001_0100.s0039_combination_sum.solution.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Medium #Top_100_Liked_Questions #Array #Backtracking #Algorithm_II_Day_10_Recursion_Backtracking
// #Level_2_Day_20_Brute_Force/Backtracking #Udemy_Backtracking/Recursion
// #Big_O_Time_O(2^n)_Space_O(n+2^n) #2023_09_29_Time_65_ms_(86.86%)_Space_45.8_MB_(75.98%)
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = []
const path: number[] = []
const comFunct = (index: number, sum: number) => {
if (sum === target) {
result.push([...path])
return
}
if (sum > target) return
for (let i = index; i < candidates.length; i++) {
path.push(candidates[i])
comFunct(i, sum + candidates[i])
path.pop()
}
}
comFunct(0, 0)
return result
}
export { combinationSum }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy