Skip to main content

Min Cost Climbing Stairs

Easy Subject: 1-D Dynamic Programming
Time Complexity
O(N)
Space Complexity
O(1)

Problem Description

Problem Statement

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

Optimal Solution

Python

Approach: Bottom-Up DP

The minimum cost to reach step i is cost[i] + min(cost to reach i-1, cost to reach i-2). We can modify the array in place to store these cumulative minimums.

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        for i in range(2, len(cost)):
            cost[i] += min(cost[i - 1], cost[i - 2])

        return min(cost[-1], cost[-2])