Created
June 8, 2021 06:26
-
-
Save vrat28/0e939ba4f72179d92ddf7cb15259c01f to your computer and use it in GitHub Desktop.
Min cost climbing stairs
This file contains 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, 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