Last active
September 27, 2020 07:02
-
-
Save jonathanagustin/c7c55ca3d165aba2aeb1d7b54a224fa6 to your computer and use it in GitHub Desktop.
746. Min Cost Climbing Stairs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def minCostClimbingStairs(self, costs: List[int]) -> int: | |
| n = len(costs) | |
| if n == 2: | |
| return min(costs[0], costs[1]) | |
| dp = [0] * n | |
| dp[0] = costs[0] | |
| dp[1] = costs[1] | |
| # At every step, we can use the previous step or 2 steps | |
| # to calculate the optimal cost at the current step | |
| for i in range(2, n): | |
| dp[i] = costs[i] + min(dp[i-1], dp[i-2]) | |
| return min(dp[-1], dp[-2]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment