Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Min Cost Climbing Stairs

Easy Acceptance 65.21% Points 20.00

You are given an integer array cost where cost[i] is the cost of stepping on the ith stair. You can climb either one or two steps at a time, and you may start from either index 0 or index 1.

Your task is to return the minimum cost to reach the top of the staircase (just beyond the last stair).

Examples
Example 1

image Input: cost = [10,15,20]
Output: 15
Explanation: Start at index 1, pay 15, and take two steps to reach the top.

Example 2

image Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: Stepping on indices 0, 2, 3, 4, 6, 7, and 9 gives a total cost of 6.

Example 3

image Input: cost = [0,0]
Output: 0
Explanation: Both stairs cost nothing, so the top is reached at zero cost.

Hints
Hint 1
  • Hint 1: Consider the minimum cost required to reach each position.
Hint 2
  • Hint 2: For every position, think about the possible ways you could have arrived there.
Hint 3
  • Hint 3: Keep track of only the information that is necessary to determine the next minimum cost.
Constraints
    • 2 = cost.length = 105
    • 0 = cost[i] = 999
Companies
Samsung Sprinklr
Topics
Dynamic Programming
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions