#746
Min Cost Climbing Stairs
easy· 1-D DPruns: 0You are given an integer array cost where cost[i] is the cost of stepping on the ith stair. Once you pay the cost, you can climb either one or two stairs. You can start from the stair with index 0 or 1. Return the minimum cost to reach the top.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 230
class Solution:
def minCostClimbingStairs(self, cost: list[int]) -> int:
n = len(cost)
a, b = 0, 0
for i in range(2, n + 1):
a, b = b, min(b + cost[i - 1], a + cost[i - 2])
return b
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.