C++11 stl solution


  • 0
    K
      int maxProfit(vector<int> &prices) {
    struct acc {
      int minPrice = numeric_limits<int>::max();
      int maxProfit = 0;
    };
    
    return std::accumulate(prices.begin(), prices.end(), acc{},
                               [](acc a, int price) {
      a.minPrice = min(a.minPrice, price);
      a.maxProfit = max(a.maxProfit, price - a.minPrice);
      return a;
    }).maxProfit;
    

    }


Log in to reply
 

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