class Solution {
public:
int maxProfit(vector<int> &prices) {
if ( prices.size() < 2 ) return 0;
int min,profit=0;
min = prices[0];
for (int i = 0; i < prices.size(); i++)
{
if (prices[i]<min)
min = prices[i];
else if(prices[i]-min > profit)
profit = prices[i]-min;
}
return profit;
}
};
public:
int maxProfit(vector<int> &prices) {
if ( prices.size() < 2 ) return 0;
int min,profit=0;
min = prices[0];
for (int i = 0; i < prices.size(); i++)
{
if (prices[i]<min)
min = prices[i];
else if(prices[i]-min > profit)
profit = prices[i]-min;
}
return profit;
}
};
本文介绍了一种通过编写算法来寻找最佳买卖时机的股票投资策略,旨在实现利润的最大化。通过分析历史股价数据,算法能够识别出买入和卖出的最佳时机,从而帮助投资者在股市中获取更高收益。
679

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



