Train of thought: As long as today is cheaper (profitable) than tomorrow, buy it. It is a process of wealth accumulation.

class Solution {
    public int maxProfit(int[] prices) {
        int profit = 0;
        for (int i = 1; i < prices.length; i++) {
            // It can be traded as long as the day is more expensive (profitable) than the day before
            if (prices[i] > prices[i - 1]) {
                profit += prices[i] - prices[i - 1]; }}returnprofit; }}Copy the code