class Solution {
public:
int maxProfit(vector<int>& prices)
{
int n = prices.size();
int start = prices[0];
int end = prices[1];
int max_profit = 0;
for(int i = 1; i < n; i++)
{
if(prices[i] > start )
{
end = prices[i];
max_profit += end - start;
}
start = prices[i];
}
return max_profit;
}
};
122. 买卖股票的最佳时机 II
最新推荐文章于 2024-09-15 11:40:06 发布