Java Simple solution 1ms


  • 0

    Loop through
    Find that value of i+1 is greater that that of i
    if yes, add difference to the profit.

    public int maxProfit(int[] prices) {
            int profit = 0;
            for (int i = 0; i < prices.length - 1; i++) {
                if (prices[i + 1] > prices[i]) {
                    profit += (prices[i + 1] - prices[i]);
                }
            }
            return profit;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.