|
| 1 | +# Intuition |
| 2 | +<!-- Describe your first thoughts on how to solve this problem. --> |
| 3 | +This problem is a typical dynamic programming (DP) problem. (keyword: `Fibonacci`) |
| 4 | + |
| 5 | +DP has two methods: tabulation and memoization. I'll introduce both methods. |
| 6 | +# Approach (tabulation) |
| 7 | +<!-- Describe your approach to solving the problem. --> |
| 8 | +1. Create an array to store the results. |
| 9 | +2. Initiate default values for the base cases `0` and `1`. |
| 10 | +3. While iterating through the array, fill in the values using this formula $$f(n) = f(n-1) + f(n-2)$$ |
| 11 | +# Complexity |
| 12 | +## Complexity (V1) |
| 13 | +- Time complexity: $$O(n)$$ |
| 14 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 15 | + |
| 16 | +- Space complexity: $$O(n)$$ |
| 17 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 18 | +# Complexity (V2) |
| 19 | +- Time complexity: $$O(n)$$ |
| 20 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 21 | + |
| 22 | +- Space complexity: $$O(1)$$ |
| 23 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 24 | + |
| 25 | +(n is value of `n`) |
| 26 | +# Code |
| 27 | +``` |
| 28 | +func climbStairsV1(n int) int { |
| 29 | + climbData := make([]int, n+2, n+2) |
| 30 | + climbData[0], climbData[1] = 1, 1 |
| 31 | + for s := 0; s < n; s++ { |
| 32 | + climbData[s+2] = climbData[s] + climbData[s+1] |
| 33 | + } |
| 34 | +
|
| 35 | + return climbData[n] |
| 36 | +} |
| 37 | +
|
| 38 | +func climbStairsV2(n int) int { |
| 39 | + prev, curr := 1, 1 |
| 40 | + for s := 1; s < n; s++ { |
| 41 | + prev, curr = curr, prev + curr |
| 42 | + } |
| 43 | +
|
| 44 | + return curr |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +> As you can see in `V2`, it can be optimized. Remove the array and maintain only the `prev` and `curr` values. In Golang, `Multiple Assignment` prodives syntatic sugar. |
| 49 | +
|
| 50 | +- - - |
| 51 | + |
| 52 | +# Approach (memoization) |
| 53 | +<!-- Describe your approach to solving the problem. --> |
| 54 | +1. Create an hash map (or array) to **cache** the results. |
| 55 | +2. Initialize default values to indicate unvisited nodes. (`-1`). |
| 56 | +3. Call the recursion using this formula $$f(n) = f(n-1) + f(n-2)$$. |
| 57 | +4. If there are cached results, return it. |
| 58 | +5. Pay attension to the **base case**, and always update the cache. |
| 59 | + |
| 60 | +# Complexity |
| 61 | +- Time complexity: $$O(n)$$ |
| 62 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 63 | + |
| 64 | +- Space complexity: $$O(n)$$ |
| 65 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 66 | +(n is value of `n`) |
| 67 | +# Code |
| 68 | +``` |
| 69 | +var cache map[int]int = map[int]int{} |
| 70 | +func climbStairs(n int) int { |
| 71 | + if n == 0 || n == 1 { |
| 72 | + return 1 |
| 73 | + } else if n < 0 { |
| 74 | + return 0 |
| 75 | + } else if val, ok := cache[n]; ok { |
| 76 | + return val |
| 77 | + } |
| 78 | +
|
| 79 | + ret := climbStairs(n-1) + climbStairs(n-2) |
| 80 | + cache[n] = ret |
| 81 | + return ret |
| 82 | +} |
| 83 | +``` |
0 commit comments