We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6fc270f commit f714b7bCopy full SHA for f714b7b
recursion/fibonacci-with-dp.js
@@ -9,19 +9,18 @@
9
* @return {number}
10
*/
11
function fibonacciDp(n) {
12
- if (n === 0) {
13
- return 0;
+ const dp = [0, 1];
+ if (n < 2) {
14
+ return dp[n + 1];
15
}
16
- if (n == 1) {
17
- return 1;
+ if (n === 1) {
18
+ return 1
19
-
20
- dp = [0, 1];
21
- let total = 0;
22
- for (let i = 2; i < n; i++) {
23
- total += dp[i - 1] + dp[i - 2];
24
- dp.push(total);
+
+ for (let i = 2; i <= n; i++) {
+ dp[i] = dp[i - 1] + dp[i - 2];
25
26
- return total;
+ return dp[n];
27
0 commit comments