Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Min Cost Climbing Stairs in Python
The min cost climbing stairs problem is a classic dynamic programming challenge. Given a staircase where each step has a cost, we need to find the minimum cost to reach the top. We can start from either step 0 or step 1, and from any step we can climb either one or two steps forward.
For example, if we have cost = [12, 17, 20], the minimum cost is 17. We start from step 1 (cost 17) and climb two steps to reach the top, avoiding the higher costs of steps 0 and 2.
Algorithm Steps
We use dynamic programming to solve this problem ?
- Create a DP array of the same size as cost array
- Initialize
dp[0] = cost[0] - If array has 2 or more elements, set
dp[1] = cost[1] - For each step from index 2, calculate minimum cost as
cost[i] + min(dp[i-1], dp[i-2]) - Return the minimum of the last two DP values
Implementation
class Solution:
def minCostClimbingStairs(self, cost):
dp = [0] * len(cost)
dp[0] = cost[0]
if len(cost) >= 2:
dp[1] = cost[1]
for i in range(2, len(cost)):
dp[i] = cost[i] + min(dp[i-1], dp[i-2])
return min(dp[-1], dp[-2])
# Test the solution
solution = Solution()
result = solution.minCostClimbingStairs([12, 17, 20])
print(f"Minimum cost: {result}")
Minimum cost: 17
How It Works
Let's trace through the example [12, 17, 20] ?
def minCostClimbingStairs(cost):
dp = [0] * len(cost)
print(f"Initial DP: {dp}")
dp[0] = cost[0]
print(f"After dp[0] = cost[0]: {dp}")
dp[1] = cost[1]
print(f"After dp[1] = cost[1]: {dp}")
for i in range(2, len(cost)):
dp[i] = cost[i] + min(dp[i-1], dp[i-2])
print(f"dp[{i}] = {cost[i]} + min({dp[i-1]}, {dp[i-2]}) = {dp[i]}")
result = min(dp[-1], dp[-2])
print(f"Final result: min({dp[-1]}, {dp[-2]}) = {result}")
return result
# Test with example
minCostClimbingStairs([12, 17, 20])
Initial DP: [0, 0, 0] After dp[0] = cost[0]: [12, 0, 0] After dp[1] = cost[1]: [12, 17, 0] dp[2] = 20 + min(17, 12) = 32 Final result: min(32, 17) = 17
Space-Optimized Solution
Since we only need the previous two values, we can optimize space complexity ?
def minCostClimbingStairs(cost):
if len(cost) <= 1:
return 0
prev2 = cost[0]
prev1 = cost[1]
for i in range(2, len(cost)):
current = cost[i] + min(prev1, prev2)
prev2 = prev1
prev1 = current
return min(prev1, prev2)
# Test the optimized solution
print(minCostClimbingStairs([12, 17, 20]))
print(minCostClimbingStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1]))
17 6
Time and Space Complexity
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| DP Array | O(n) | O(n) |
| Space Optimized | O(n) | O(1) |
Conclusion
The min cost climbing stairs problem demonstrates dynamic programming principles effectively. The key insight is that at each step, we choose the cheaper path from the previous one or two steps. The space-optimized version reduces memory usage while maintaining the same time complexity.
