
求解代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
int[] memo = new int[n+1];
Arrays.fill(memo, -1);
return dp(cost,n, memo);
}
private int dp(int[] cost,int n,int[] memo){
if(n==0||n==1){
return 0;
}
if(memo[n]!=-1){
return memo[n];
}
memo[n]=Math.min(dp(cost, n-1, memo)+cost[n-1],dp(cost, n-2, memo)+cost[n-2]);
return memo[n];
}
|
小贴士
所有的钱,都花在落脚的那一刻;跳的过程,一分钱不花。