AlgoMaster Logo

Best Time to Buy and Sell Stock II

prices=[7, 1, 5, 3, 6, 4]
1public int maxProfit(int[] prices) {
2    int maxProfit = 0;
3
4    for (int i = 1; i < prices.length; i++) {
5        if (prices[i] > prices[i - 1]) {
6            maxProfit += prices[i] - prices[i - 1];
7        }
8    }
9
10    return maxProfit;
11}
0 / 12
012345715364