mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 15:11:16 +08:00
parent
941feda305
commit
7c9601df3e
@ -15,20 +15,20 @@ function combinationSumRecursive(
|
|||||||
) {
|
) {
|
||||||
if (remainingSum < 0) {
|
if (remainingSum < 0) {
|
||||||
// By adding another candidate we've gone below zero.
|
// By adding another candidate we've gone below zero.
|
||||||
// This would mean that last candidate was not acceptable.
|
// This would mean that the last candidate was not acceptable.
|
||||||
return finalCombinations;
|
return finalCombinations;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (remainingSum === 0) {
|
if (remainingSum === 0) {
|
||||||
// In case if after adding the previous candidate out remaining sum
|
// If after adding the previous candidate our remaining sum
|
||||||
// became zero we need to same current combination since it is one
|
// became zero - we need to save the current combination since it is one
|
||||||
// of the answer we're looking for.
|
// of the answers we're looking for.
|
||||||
finalCombinations.push(currentCombination.slice());
|
finalCombinations.push(currentCombination.slice());
|
||||||
|
|
||||||
return finalCombinations;
|
return finalCombinations;
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case if we haven't reached zero yet let's continue to add all
|
// If we haven't reached zero yet let's continue to add all
|
||||||
// possible candidates that are left.
|
// possible candidates that are left.
|
||||||
for (let candidateIndex = startFrom; candidateIndex < candidates.length; candidateIndex += 1) {
|
for (let candidateIndex = startFrom; candidateIndex < candidates.length; candidateIndex += 1) {
|
||||||
const currentCandidate = candidates[candidateIndex];
|
const currentCandidate = candidates[candidateIndex];
|
||||||
|
Loading…
Reference in New Issue
Block a user