Check this out guys. My JAVA Solution.


  • 0
    A
    class Solution {
        public int maxProfit(int[] prices) {
            int buyingPrice = Integer.MAX_VALUE;
            int profit = 0;
            for(int i = 0; i < prices.length; i++){
                int currentPrice = prices[i];
                if(currentPrice < buyingPrice){
                    buyingPrice = currentPrice;
                } else if(currentPrice - buyingPrice > profit){
                    profit = currentPrice - buyingPrice;
                }
            }
            return profit;        
        }
    }
    

Log in to reply
 

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