121. 买卖股票的最佳时机

class Solution {
public:
    int maxProfit(vector<int>& prices) {
    int counter=0;int ans=0;
for(int i=1;i<prices.size();i++)
{
    counter+=prices[i]-prices[i-1];
    if(counter<0)
    {
        counter=0;
    }
     ans=max(counter,ans);
}
    return ans;
    }
};
posted @ 2022-03-01 21:05  fengmao31  阅读(26)  评论(0编辑  收藏  举报