题解 | #最小花费爬楼梯# | C++
最小花费爬楼梯
https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; class Solution { public: int minCost(const std::vector<int>&costs, int n) { int c = 0; if (n <= 1) return c; int costs_one = costs[0], costs_two = costs[1]; for(int i = 2; i < n; i++) { c = std::min(costs_one, costs_two); costs_one = costs_two; costs_two = c + costs[i]; } return std::min(costs_one, costs_two); } }; int main() { int n; std::vector<int> costs; std::cin >> n; int i=1; while (i <= n) { int p; std::cin >> p; costs.push_back(p); i++; } std::cout << Solution().minCost(costs, n); } // 64 位输出请用 printf("%lld")