mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Update combinationSum README to reflect new implementation
This commit is contained in:
parent
f6408ffb57
commit
2f60cab835
@ -38,21 +38,22 @@ A solution set is:
|
|||||||
## Explanations
|
## Explanations
|
||||||
|
|
||||||
Since the problem is to get all the possible results, not the best or the
|
Since the problem is to get all the possible results, not the best or the
|
||||||
number of result, thus we don’t need to consider DP (dynamic programming),
|
number of result, we don’t need to consider dynamic programming.
|
||||||
backtracking approach using recursion is needed to handle it.
|
We do instead a depth-first traversal of the decision tree,
|
||||||
|
using an iterative implementation to avoid stack overflows.
|
||||||
|
|
||||||
Here is an example of decision tree for the situation when `candidates = [2, 3]` and `target = 6`:
|
Here is an example of decision tree for the situation when `candidates = [2, 3]` and `target = 6`:
|
||||||
|
|
||||||
```
|
```
|
||||||
0
|
0
|
||||||
/ \
|
/ \
|
||||||
+2 +3
|
+3 +2
|
||||||
/ \ \
|
/ \ \
|
||||||
+2 +3 +3
|
+3 +2 +2
|
||||||
/ \ / \ \
|
/ \ \
|
||||||
+2 ✘ ✘ ✘ ✓
|
✓ +2 +2
|
||||||
/ \
|
\ \
|
||||||
✓ ✘
|
✘ ✓
|
||||||
```
|
```
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
Loading…
Reference in New Issue
Block a user