This problem is a typical dynamic programming (DP) problem. (keyword: Fibonacci
)
DP has two methods: tabulation and memoization. I'll introduce both methods.
- Create an array to store the results.
- Initiate default values for the base cases
0
and1
. - While iterating through the array, fill in the values using this formula
$$f(n) = f(n-1) + f(n-2)$$
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(n)$$
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(1)$$
(n is value of n
)
func climbStairsV1(n int) int {
climbData := make([]int, n+2, n+2)
climbData[0], climbData[1] = 1, 1
for s := 0; s < n; s++ {
climbData[s+2] = climbData[s] + climbData[s+1]
}
return climbData[n]
}
func climbStairsV2(n int) int {
prev, curr := 1, 1
for s := 1; s < n; s++ {
prev, curr = curr, prev+curr
}
return curr
}
As you can see in
V2
, it can be optimized. Remove the array and maintain only theprev
andcurr
values. In Golang,Multiple Assignment
prodives syntatic sugar.
- Create an hash map (or array) to cache the results.
- Initialize default values to indicate unvisited nodes. (
-1
). - Call the recursion using this formula
$$f(n) = f(n-1) + f(n-2)$$ . - If there are cached results, return it.
- Pay attension to the base case, and always update the cache.
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(n)$$
(n is value of n
)
var cache map[int]int = map[int]int{}
func climbStairs(n int) int {
if n == 0 || n == 1 {
return 1
} else if n < 0 {
return 0
} else if val, ok := cache[n]; ok {
return val
}
ret := climbStairs(n-1) + climbStairs(n-2)
cache[n] = ret
return ret
}