mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0746.使用最小花费爬楼梯.md
This commit is contained in:
@ -203,7 +203,26 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
```Java
|
||||
class Solution {
|
||||
public int minCostClimbingStairs(int[] cost) {
|
||||
if (cost == null || cost.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (cost.length == 1) {
|
||||
return cost[0];
|
||||
}
|
||||
int[] dp = new int[cost.length];
|
||||
dp[0] = cost[0];
|
||||
dp[1] = cost[1];
|
||||
for (int i = 2; i < cost.length; i++) {
|
||||
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
|
||||
}
|
||||
//最后一步,如果是由倒数第二步爬,则最后一步的体力花费可以不用算
|
||||
return Math.min(dp[cost.length - 1], dp[cost.length - 2]);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user