题解 | #最小花费爬楼梯#
最小花费爬楼梯
http://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e
Java动态规划----最小花费爬楼梯
import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] cost = new int[n];
for (int i = 0; i < n; i++) {
cost[i] = sc.nextInt();
}
System.out.println(minmumCost(n, cost));
}
private static int minmumCost(int n, int[] cost) {
int[] minCost = new int[n];
minCost[0] = 0;
minCost[1] = Math.min(cost[0], cost[1]);
for (int i = 2; i < n; i++) {
minCost[i] = Math.min(minCost[i - 2] + cost[i - 1], minCost[i - 1] + cost[i]);
}
return minCost[n - 1];
}
}