1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.demo.s121;
public class Solution { public int maxProfit(int[] prices) { int minBuy = Integer.MAX_VALUE; int maxProfit = 0; for(int i = 0; i< prices.length; i++) { minBuy = Math.min(minBuy, prices[i]); maxProfit = Math.max(maxProfit, prices[i] - minBuy); } return maxProfit; } }
|