class Solution { public int maxProfit(int[] prices) { int maxProfit = 0; int prevBuy = Integer.MAX_VALUE; int maxSellPrice = Integer.MIN_VALUE; for (int buyDay=0; buyDay < prices.length; buyDay++) { int buyPrice = prices[buyDay]; if (buyPrice >= prevBuy && maxSellPrice >= buyPrice) { continue; } maxSellPrice = prices[buyDay]; for (int sellDay = buyDay+1; sellDay < prices.length; sellDay++) { maxSellPrice = Math.max(maxSellPrice, prices[sellDay]); } maxProfit = Math.max(maxProfit, maxSellPrice-buyPrice); prevBuy = buyPrice; } return maxProfit; } }