Practice Problem
Min Cost Climbing Stairs
Difficulty: Easy
Given an array of step costs, find the minimum cost to reach the top of the staircase starting from step 0 or step 1.
Min Cost Climbing Stairs
You are given an integer array cost where cost[i] is the cost of the i-th step on a staircase. Once you pay the cost, you can either climb 1 or 2 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.
The top of the floor is one step beyond the last step (i.e., index cost.length).
Examples
Example 1:
Input: cost = [10, 15, 20]
Output: 15
Explanation: You start at step 1 (cost 15), then climb 2 steps to reach the top.
Total cost: 15.Example 2:
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Start at step 0 and follow the cheapest path:
Step 0 (cost 1) -> Step 2 (cost 1) -> Step 3 (cost 1) -> Step 5 is expensive,
so Step 4 (cost 1) -> Step 6 (cost 1) -> Step 7 (cost 1) -> Top.
Total cost: 1 + 1 + 1 + 1 + 1 + 1 = 6.Constraints
2 <= cost.length <= 10000 <= cost[i] <= 999
Expected Complexity
- Time: O(n)
- Space: O(1)
EASY
Dynamic Programming
Tabulation
Beginner
0 views
Solution
Hints
Hint 1
Hint 2
Premium
Hint 3
Premium
Hint 4
Premium
This section is available for CodeSnatch Premium members only.
