746 Min Cost Climbing Stairs
1. Question
On a staircase, thei
-th step has some non-negative costcost[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:
Example 2:
Note:
cost
will have a length in the range[2, 1000]
.Every
cost[i]
will be an integer in the range[0, 999]
.
2. Implementation
(1) DP
思路: 由于我们可以选择从index 0或者index 1开始,所以对于位置i来说,产生的cost有两种情况,状态转移为:
f(i) = cost(i) + min(f(i - 1), f(i - 2)); 其中f(i)表示在位置i上最小的cost
3. Time & Space Complexity
DP:时间复杂度O(n), 空间复杂度O(n)
Last updated
Was this helpful?