2. Min cost climbing stairs
You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
Example 1:
Input: cost = [10,15,20] Output: 15 Explanation: You will start at index 1. - Pay 15 and climb two steps to reach the top. The total cost is 15.
Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1] Output: 6 Explanation: You will start at index 0. - Pay 1 and climb two steps to reach index 2. - Pay 1 and climb two steps to reach index 4. - Pay 1 and climb two steps to reach index 6. - Pay 1 and climb one step to reach index 7. - Pay 1 and climb two steps to reach index 9. - Pay 1 and climb one step to reach the top. The total cost is 6.
Answer:
If we start with a brute-force approach, we can construct a decision tree where each node represents a step, and from each step, you can either take 1 step or 2 steps forward. By exploring every possible path in this tree, we can find all potential jump routes and determine the minimum total cost to reach the final step.
Since each step branches into two possibilities, the total number of recursive calls doubles at every level. Therefore, the time complexity of this brute-force solution is O(2ⁿ).
| Brute Force: Decision Tree |
Optimal Substructure:
Dynamic Programming (DP) leverages the optimal substructure property, meaning the solution to a problem can be built from solutions to its subproblems.
In a brute-force approach, many recursive calls are repeated multiple times. Dynamic Programming helps eliminate this redundancy by storing the results of these overlapping subproblems in a memoization structure (like an array or dictionary), instead of recalculating them each time.
| Optimal sub-structure |
For example, in the decision tree above, the route 2 → 3 (weight = 20) is computed more than once. By storing this result in an array, we can reuse it whenever needed.
By converting the recursive solution into a bottom-up DP approach, we reduce the time complexity from O(2ⁿ) to O(n)— at the cost of O(n) additional space.
From exponential to linear, that’s the power of Dynamic Programming!
Bottum-up Solution:
If you start from the last position of the cost array, there’s only one possible move - to stay at that position.
So, the minimum cost at the last index is simply the cost of that step, i.e., 20.
| index 1 |
Starting at index 1
From index 1, you have two choices:
Take 1 step → total cost = 15 (current step) + 20 (next step) = 35
Take 2 steps → total cost = 15 (current step) + 0 (since you jump beyond the last step) = 15
The minimum of these two values is 15, which we store in the answer array.
| start at index 0 |
Starting at index 0
From index 0, you again have two options:
Take 1 step → total cost = 10 (current step) + 15 (cost from index 1) = 25
Take 2 steps → total cost = 10 (current step) + 20 (cost from index 2) = 30
The minimum of these values is 25, which we store in the answer array.
Final Step
The overall minimum cost to reach the top is:
Final Answer: 15
Comments
Post a Comment