Consice Solution in Java


  • 1
    H
    public class Solution {
        public int maxProfit(int[] prices) {
            int p = 0;
            if(prices.length<1)
              return 0;
            int min = prices[0];
            for (int i = 0; i < prices.length; i++) {
                if (prices[i]>=min){
                    p = Math.max(prices[i]-min,p);
                }else {
                    min = prices[i];
                }
            }
            return p;
        }
    }

Log in to reply
 

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