classSolution{public:intmaxProfit(vector<int>&prices){intn=prices.size();inttotalProfit=0;// this will store our maximum total profit// We will start looking from the second element. Why?// we need a element before, to compare with itfor(inti=1;i<n;i++){// If this current price, is larger than the previous price// We gain a profit thenif(prices[i]>prices[i-1]){// Add this profit to our `totalProfit`totalProfit+=prices[i]-prices[i-1];// totalProfit = Selling Price - Buying Price}// else, Do nothing}returntotalProfit;}};
Warning
LINK
You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue?