You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
new solution update 2022.02.07
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intmaxProfit(vector<int>& p){ int res = 0; for(int buy = 0; buy < p.size();) { int sell = buy + 1; for(; sell < p.size() and p[sell] >= p[sell - 1]; sell++) {} res += max(0, p[sell] - p[buy]); buy = sell; } return res; } };