Java / One pass with updating min and profit / T : O(N), S : O(1)


  • 0
    J
    public class Solution {
        public int maxProfit(int[] prices) {
            int profit = 0;
            int min = Integer.MAX_VALUE;
            for(int price : prices){
                if(min > price){
                    min = price;
                }
                if(profit < (price - min)){
                    profit = (price - min);
                }
            }
            return profit;
        }
    }
    

Log in to reply
 

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