MJay

736 - DP - Stairs 본문

Programming/LeetCode

736 - DP - Stairs

MJSon 2019. 10. 21. 09:56

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20]

Output: 15

Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

 

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]

Output: 6

Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].



class Solution(object):

   def minCostClimbingStairs(self, cost):

       """

       :type cost: List[int]

       :rtype: int

       """

       N = len(cost)

       cost.append(0)

       dp = [0] * (N + 1)

       dp[0] = cost[0]

       dp[1] = cost[1]

       for i in range(2, N + 1):

           dp[i] = min(dp[i - 1] +cost[i], dp[i - 2] + cost[i])

       return dp[-1]

 

a = Solution()

 

print(a.minCostClimbingStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1]))

 

'Programming > LeetCode' 카테고리의 다른 글

256 - DP - Paint House  (0) 2019.10.21
392 - DP - Subsequence  (0) 2019.10.21
[D&Q] - 973. K Closest Points to Origin  (0) 2019.10.16
[DP] - 121. Best Time to Buy and Sell Stock  (0) 2019.10.16
[DP] - 53. Maximum Subarray  (2) 2019.10.10