746. Min Cost Climbing Stairs
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 67% Topics: Array, Dynamic Programming
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
dp = [0] * 3
for i in reversed(xrange(len(cost))):
dp[i%3] = cost[i] + min(dp[(i+1)%3], dp[(i+2)%3])
return min(dp[0], dp[1])
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions