Best Time to Buy and Sell Stock II LeetCode solution in C++
1class Solution {2public:3int maxProfit(vector<int> &prices) {4int n = prices.size();5int ans = 0, currMin = prices[0];6for (int i = 1; i < n; i++) {7while (i < n && prices[i] > prices[i - 1]) {8i++;9}10ans += (prices[i - 1] - currMin);11currMin = prices[i];12}13return ans;14}15};