मुख्य कंटेंट तक स्किप करें
Back to ChallengesMin Cost Climbing StairsEasy 15 min

Min Cost Climbing Stairs

You are given an integer array cost where cost[i] is the cost of i-th step on a staircase. Once you pay the cost, you can climb one or two steps. You can start from index 0 or 1. Return the minimum cost to reach the top.

Examples

Input: cost = [10, 15, 20]
Output: 15
Start at index 1, pay 15, and climb two steps to reach the top.
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Take the path: 0 -> 2 -> 4 -> 6 -> 7 -> 9 for total cost 6.

Constraints

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

Complexity Analysis

Time
O(n)
single pass over the cost array.
Space
O(1)
optimized space using two variable trackers.

Test Cases

#1 Simple 3-stair case
Input: [10, 15, 20]
Expected: 15
#2 Alternating small/large costs
Input: [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Expected: 6
JavaScript
Output
Click "Run Code" to see output here...