Best Time to Buy and Sell Stock atmost B times Time : Space : 12345678910111213141516int Solution::solve(vector<int> &A, int B) { int n = A.size(), res = 0; vector<int> dp(n + 1); for(int i = 0; i < min(n,B); i++) { vector<int> dpp(n + 1); int ma = 0; for(int j = n - 1; j >= 0; j--) { dpp[j] = max({dp[j], ma - A[j], dpp[j+1]}); ma = max(ma, dp[j + 1] + A[j]); res = max(res, dpp[j]); } swap(dp,dpp); } return res;}