10 line java solution


  • 0
    S
    public class Solution {
        public int maxProfit(int[] prices) {
            int len = prices.length;
            if(len<=1) return 0;
            int max = 0;
            int low = prices[0];
            for(int i=1; i<len; i++){
                max = prices[i]-low>max?prices[i]-low:max;
                low = prices[i]<low?prices[i]:low;
            }
            return max;
        }
    }

Log in to reply
 

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