Java O(n) answer


  • 0
    S
    public class Solution {
        public int maxProfit(int[] prices) {
           int maxProfix=0,minPrice=Integer.MAX_VALUE;
           for(int i=0;i<prices.length;i++){
        	   minPrice=(prices[i]<minPrice)?prices[i]:minPrice;
        	   maxProfix=(prices[i]-minPrice>maxProfix)?(prices[i]-minPrice):maxProfix;
           }
           return maxProfix;
        }
    }

Log in to reply
 

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