AlgoMaster Logo

Min Cost Climbing Stairs

cost=[10, 15, 20]
1public int minCostClimbingStairs(int[] cost) {
2    int n = cost.length;
3    int[] dp = new int[n + 1];
4
5    for (int i = 2; i <= n; i++) {
6        dp[i] = Math.min(dp[i - 1] + cost[i - 1],
7                         dp[i - 2] + cost[i - 2]);
8    }
9
10    return dp[n];
11}
0 / 9
cost:010115220dp:0?1?2?3?