Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created June 8, 2021 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/0e939ba4f72179d92ddf7cb15259c01f to your computer and use it in GitHub Desktop.
Save vrat28/0e939ba4f72179d92ddf7cb15259c01f to your computer and use it in GitHub Desktop.
Min cost climbing stairs
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
# The array's length should be 1 longer than the length of cost
# This is because we can treat the "top floor" as a step to reach
minimum_cost = [0] * (len(cost) + 1)
# Start iteration from step 2, since the minimum cost of reaching
# step 0 and step 1 is 0
for i in range(2, len(cost) + 1):
take_one_step = minimum_cost[i - 1] + cost[i - 1]
take_two_steps = minimum_cost[i - 2] + cost[i - 2]
minimum_cost[i] = min(take_one_step, take_two_steps)
# The final element in minimum_cost refers to the top floor
return minimum_cost[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment