Skip to content
Prev Previous commit
Next Next commit
8.01 tripleStep O(1) or constant space
  • Loading branch information
Carlos Green committed Nov 7, 2020
commit a8b15b9d193065ca2988da2b2a8e8c3f679663cd
30 changes: 30 additions & 0 deletions chapter08/8.01 - Triple Step/tripleStep.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
Recursion and Dynamic Programming (4 steps)
1. Start with the recursive backtracking solution (Brute Force)
2. Optimize by using a memoization table (top-down dynamic programming)
3. Remove the need for recursion (bottom-up dynamic programming)
4. Apply final tricks to reduce the time / memory complexity
*/

/*
// Recursive BackTracking
// Time & Space O(n!)
var tripleStep = function(n, res=0) {
Expand Down Expand Up @@ -29,6 +38,27 @@ var tripleStep = function(n, memo=[1,1,2,4]) {

return memo[memo.length - 1]
}
*/

// Removed Memoization
// Time O(n) & Space O(1)
var tripleStep = function(n) {
if (n <= 0) return 0
if (n === 1) return 1
if (n === 2) return 2

let a = 1
let b = 1
let c = 2

for (let i = 3; i < n; i++) {
let d = a + b + c
a = b
b = c
c = d
}
return a + b + c
}

/* TEST */
console.log(tripleStep(1), 1);
Expand Down