C++ , you should know how to solve the problem


  • 0
    B

    class Solution {
    public:
    int maxProfit(vector<int>& prices) {
    if(prices.empty())
    return 0;
    int leng = prices.size();
    int minPrice = prices[0];
    int maxPro = 0 ;

             for(int i =1;i<leng;i++)
             {
                 if(prices[i]<minPrice)
                     minPrice = prices[i];
    
                 if(maxPro <(prices[i] - minPrice))
                     maxPro = prices[i] - minPrice ;
             }
             return maxPro;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.