目錄
題目
解法一
題目
解法一
int min(int a, int b) {return a < b ? a : b;
}int minCostClimbingStairs(int* cost, int costSize) {const int n = costSize; // 樓頂,第n階// 爬到第n階的最小花費 = // 爬到第n-1階的最小花費+從第n-1階爬上第n階的花費和// 爬到第n-2階的最小花費+從第n-2階爬上第n階的花費中取最小值int ppre = 0, pre = 0, cur;for (int i = 2; i <= n; i++) {cur = min(pre + cost[i - 1], ppre + cost[i - 2]);ppre = pre;pre = cur;}return cur;
}