Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
Last active September 27, 2020 07:02
Show Gist options
  • Select an option

  • Save jonathanagustin/c7c55ca3d165aba2aeb1d7b54a224fa6 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanagustin/c7c55ca3d165aba2aeb1d7b54a224fa6 to your computer and use it in GitHub Desktop.
746. Min Cost Climbing Stairs
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