C++ solution,13ms


  • 0
    J

    int maxProfit(vector<int> &prices) {
    if(prices.size()<2){
    return 0;
    }
    int maxProfit = 0;
    int minPrice = prices[0];
    int curProfit = 0;
    for(int i=0;i<prices.size();i++){
    curProfit = prices[i]-minPrice;
    if(curProfit>maxProfit){
    maxProfit = curProfit;
    }
    if(prices[i]<minPrice){
    minPrice = prices[i];
    }
    }
    return maxProfit;
    }


Log in to reply
 

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