Very easy java solution


  • 1
    A
    public class Solution {
        public int maxProfit(int[] prices) {
            int sum = 0;
            int cur = 0;
            for(int i = 1; i < prices.length; i++){
                int tmp = prices[i] - prices[i - 1];
                cur = cur > 0 ? cur + tmp : tmp;
                sum = sum > cur ? sum : cur;
            }
            return sum;
        }
    }

  • -1
    B

    cur = cur > 0 ? cur + tmp : tmp;
    should be:

    cur = cur > 0 ? cur + tmp : 0;


  • 0
    A

    Thanks, But obviously, it should be tmp, you can try [5, 3, 9] as a input


Log in to reply
 

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