class Solution {
public:
int maxProfit(vector<int> &a) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int minIdx = 0;
int diff = 0;
int res = 0;
for (int i = 1; i < a.size(); ++i)
{
if (a[minIdx] > a[i])
minIdx = i;
if (a[i] - a[minIdx] > res)
{
res = a[i] - a[minIdx];
}
}
return res;
}
};[Leetcode] Best Time to Buy and Sell Stock
最新推荐文章于 2023-02-28 14:37:52 发布
本文介绍了一个使用C++实现的股票交易算法,通过滑动窗口技术寻找最大利润。该算法遍历股票价格数组,确定买入和卖出的最佳时机,以获取最大收益。
679

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



