09 Sept 2026Go / Python / TypeScriptEasy

Min Cost Climbing Stairs

Find the minimum cost needed to move beyond the top of a staircase when each move climbs one or two steps.

Work backward while retaining only the optimal costs from the next two positions, then choose the cheaper starting step.

complexity

O(n) time and O(1) extra space.

solution files

  • Go min-cost-climbing-stairs/solution.go
  • Python min-cost-climbing-stairs/solution.py
  • TypeScript min-cost-climbing-stairs/solution.ts

Solution files

Gomin-cost-climbing-stairs/solution.go
package main

func minCostClimbingStairs(cost []int) int {
	oneAhead, twoAhead := 0, 0
	for index := len(cost) - 1; index >= 0; index-- {
		current := cost[index] + minCost(oneAhead, twoAhead)
		twoAhead, oneAhead = oneAhead, current
	}
	return minCost(oneAhead, twoAhead)
}

func minCost(a int, b int) int {
	if a < b {
		return a
	}
	return b
}
Pythonmin-cost-climbing-stairs/solution.py
class Solution:
    def minCostClimbingStairs(self, cost: list[int]) -> int:
        one_ahead = two_ahead = class="syntax-number">0
        for value in reversed(cost):
            current = value + min(one_ahead, two_ahead)
            two_ahead, one_ahead = one_ahead, current
        return min(one_ahead, two_ahead)
TypeScriptmin-cost-climbing-stairs/solution.ts
function minCostClimbingStairs(cost: number[]): number {
  let oneAhead = class="syntax-number">0;
  let twoAhead = class="syntax-number">0;
  for (let index = cost.length - class="syntax-number">1; index >= class="syntax-number">0; index -= class="syntax-number">1) {
    const current = cost[index] + Math.min(oneAhead, twoAhead);
    twoAhead = oneAhead; oneAhead = current;
  }
  return Math.min(oneAhead, twoAhead);
}