当前位置:网站首页>Force deduction 121 questions

Force deduction 121 questions

2022-06-13 04:45:00 Look at the mountains_ Lau

class Solution 
{
    
public:
    int maxProfit(vector<int>& prices) 
    {
    
        if(prices.empty())
            return 0;
        // Direct use of a loop , Find the lowest price to buy , And find the maximum profit under this buying price 
        int profit  = 0;
        int min = prices[0];
        for(int i = 1; i < prices.size(); i++)
        {
    
            if(min > prices[i])
                min = prices[i];
            else
                profit = max(profit, prices[i] - min);
        }
        return profit;
        
    }
};

原网站

版权声明
本文为[Look at the mountains_ Lau]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280521126143.html