c++ binary 7lines O(nlogn)


  • 0
    T

    int maxProfit(vector<int>& prices) {
    if(prices.size()<2)
    return 0;
    int res=*max_element(prices.begin()+prices.size()/2,prices.end())-*min_element(prices.begin(),prices.begin()+prices.size()/2);
    vector<int> temp1(prices.begin(),prices.begin()+prices.size()/2);
    vector<int> temp2(prices.begin()+prices.size()/2,prices.end());
    return max(max(maxProfit(temp1),maxProfit(temp2)),res);
    }


Log in to reply
 

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