class Solution {
public:
int maxProfit(vector<int> &a) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int prevIdx = 0;
int res = 0;
for (int i = 1; i < a.size(); ++i)
{
if (a[i] > a[prevIdx])
{
res += a[i] - a[prevIdx];
}
prevIdx = i;
}
return res;
}
};[Leetcode] Best Time to Buy and Sell Stock II
最新推荐文章于 2023-02-28 14:37:52 发布
本文介绍了一种用于计算股票买卖最大利润的C++算法实现。该算法通过遍历价格数组,当遇到价格上涨时即刻卖出以获取利润,最终返回累积的最大利润。此方法简单高效,适用于非连续交易日的价格波动分析。
310

被折叠的 条评论
为什么被折叠?



