Java Simple Solution with concept of min and max (1 ms)


  • 0
    R

    public int maxProfit(int[] prices)
    {

      int max = 0; 
      if(prices.length < 1)
            return max; 
       
       int min = prices[0];       
       for(int i=1;i<prices.length;i++)
       {
            if(prices[i] <= min)
                min = prices[i];
            else
            {
                int diff= prices[i] - min;
                if(diff >= max)
                    max = diff;
            }
       }
       return max;
    

    }


Log in to reply
 

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