题解 | 最小花费爬楼梯
最小花费爬楼梯
https://www.nowcoder.com/practice/6fe0302a058a4e4a834ee44af88435c7
暴力递归解法
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param cost int整型一维数组 * @return int整型 */ public int minCostClimbingStairs (int[] cost) { int[] dp = new int[cost.length]; return Math.min(process(cost, 0, dp), process(cost, 1, dp)); } private int process(int[] cost, int index, int[] dp) { if (dp[index] != 0) { return dp[index]; } if (index == cost.length - 1 || index == cost.length - 2) { dp[index] = cost[index]; return dp[index] ; } int p1 = process(cost, index + 1, dp) + cost[index]; int p2 = process(cost, index + 2, dp) + cost[index]; dp[index] = Math.min(p1, p2); return dp[index]; } }