1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution {
public int maxProfit(int[] prices) { int ans = 0; int min = prices[0]; for (int i = 1; i < prices.length; i++) { int x = prices[i]; if (x > min) { ans = Math.max(ans, x - min); } min = Math.min(min, x); } return ans; } }
|