Minimum Cost to Reach the Top of a Staircase Taking One or Two Steps

Quick Overview

Given the cost of each step on a staircase, find the cheapest way to climb past the last step when each move goes up one or two steps and you may start on either of the first two. The problem tests setting up a one-dimensional dynamic programming recurrence and reducing it to constant space.

Minimum Cost to Reach the Top of a Staircase Taking One or Two Steps

Company: Shopee

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 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. ### Function Signature ```python def min_cost_climbing_stairs(cost: list[int]) -> int: ``` ### Rules - The staircase has `n = len(cost)` steps, indexed `0` to `n - 1`. The top is position `n`, one past the last step. - You pay `cost[i]` when you leave step `i`. Starting on step `0` or step `1` is free, and reaching the top costs nothing extra. - From step `i` you move to `i + 1` or `i + 2`. Landing on position `n` from step `n - 1` or `n - 2` reaches the top. ### Constraints - `2 <= cost.length <= 1000` - `0 <= cost[i] <= 999` ### Examples ```text Input: cost = [10,15,20] Output: 15 ``` Start at index 1, pay 15, and climb two steps to reach the top. Any path that starts at index 0 costs at least 25 (10 + 15). ```text Input: cost = [1,100,1,1,1,100,1,1,100,1] Output: 6 ``` Start at index 0 and step on indices 0, 2, 4, 6, 7 and 9, paying 1 at each, then climb from index 9 to the top. This path avoids all three steps that cost 100 (indices 1, 5 and 8), for a total of 6. ```text Input: cost = [1,2,3,4] Output: 4 ``` Start at index 0, pay 1 and climb two steps to index 2, pay 3 and climb two steps to the top. The last step (cost 4) is never used.

Overview: Given the cost of each step on a staircase, find the cheapest way to climb past the last step when each move goes up one or two steps and you may start on either of the first two. The problem tests setting up a one-dimensional dynamic programming recurrence and reducing it to constant space.

Shopee logo
Shopee
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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 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.

Function Signature

def min_cost_climbing_stairs(cost: list[int]) -> int:

Rules

  • The staircase has n = len(cost) steps, indexed 0 to n - 1 . The top is position n , one past the last step.
  • You pay cost[i] when you leave step i . Starting on step 0 or step 1 is free, and reaching the top costs nothing extra.
  • From step i you move to i + 1 or i + 2 . Landing on position n from step n - 1 or n - 2 reaches the top.

Constraints

  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999

Examples

Input: cost = [10,15,20]
Output: 15

Start at index 1, pay 15, and climb two steps to reach the top. Any path that starts at index 0 costs at least 25 (10 + 15).

Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6

Start at index 0 and step on indices 0, 2, 4, 6, 7 and 9, paying 1 at each, then climb from index 9 to the top. This path avoids all three steps that cost 100 (indices 1, 5 and 8), for a total of 6.

Input: cost = [1,2,3,4]
Output: 4

Start at index 0, pay 1 and climb two steps to index 2, pay 3 and climb two steps to the top. The last step (cost 4) is never used.