Dynamic Programming (DP)
Buy and Sell Stock 121 Best Time to Buy and Sell Stock Base Case: int minPrice = prices[0]; int maxProfit = 0; Formula: for (auto i = 0; i < prices.size(); i++) { minPrice = min(minPrice, prices[i]); maxProfit = max(maxProfit, prices[i] - minPrice); } 122. Best Time to Buy and Sell Stock II Greedily Add all the prices[i] - prices[i - 1] delta to the total profit if it is greater than zero (since we can complete as much transactions as we want and there is no transaction fees) A slightly improvement is that, in a inner while loop, first record j = i as the starting price, while (i < N - 1 && prices[i] < prices[i +1]) we keep increase i, until it...