classSolution {public:intmaxProfit(vector<int>& prices) {returnprices.size() >0?helper(prices,0,prices.size() -1)[0] :0; }private: /** * index 0: max profit, index 1: max price in the range and index 2: min price in the range */vector<int> helper(constvector<int>& prices,int i,int j) { // Base case vector<int> result = {0,prices[i],prices[j]};if(i == j) return result;int m = i + (j - i) /2; vector<int> left =helper(prices, i, m); vector<int> right =helper(prices, m+1, j);result[0] =max(left[0],max(right[0],right[1] -left[2]));result[1] =max(left[1],right[1]);result[2] =min(left[2],right[2]);return result; }};