121. Best Time to Buy and Sell Stock#

Code#

 1public int maxProfit(int[] prices) {
 2  int minPrice = prices[0];
 3  int maxProfit = 0;
 4
 5  for (int i = 0; i < prices.length; i++) {
 6    minPrice = Math.min(minPrice, prices[i]);
 7    maxProfit = Math.max(maxProfit, prices[i] - minPrice);
 8  }
 9
10  return maxProfit;
11}