A jave accepted solution with O(N) time


  • 0
    L


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


Log in to reply
 

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