class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int profit = 0;
int len = prices.size();
if (len < 2) {
return profit;
}
int diff;
for (int i = 1; i < len; i++) {
diff = prices[i] - prices[i - 1];
if (diff > 0) {
profit += diff;
}
}
return profit;
}
};
LeetCode Best Time to Buy and Sell Stock II
最新推荐文章于 2023-02-28 14:37:52 发布