Leetcode 746
Min Cost Climbing Stairs
- Problem Description:
爬楼梯:一次可以爬1-2层楼梯,每爬一层楼梯会消耗对应的能量,求出爬到最高一层的楼梯时所消耗的最少能量。
具体的题目信息:
https://leetcode.com/problems/min-cost-climbing-stairs/description/ - Example:
- Solution:
class Solution { public: int minCostClimbingStairs(vector<int>& cost) { if (cost.size() == 2) return cost[1]; int a = 0, b = 0; for (int i = 0; i < cost.size(); i++) { int t = min(a, b)+cost[i]; a = b; b = t; } return min(a, b); } };