class Solution {
public:
int maxProfit(vector<int>& prices) {
int cost = 1000000,ans = 0;
for(int x : prices){
ans = max(ans,x-cost);
if(cost > x)
cost = x;
}
return ans;
}
};leetcode 121. Best Time to Buy and Sell Stock
最新推荐文章于 2025-12-04 16:02:12 发布
本文介绍了一种用于计算给定股票价格序列中所能获得的最大利润的算法。通过一次遍历股票价格数组,此算法能够有效地找到最低买入价格并计算出最大可能收益。
281

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



