```
public class Solution {
public int maxProfit(int[] prices) {
int max = 0, i = 0, j = 1;
while (i < prices.length - 1 && j < prices.length) {
if (prices[i] > prices[j]) i = j;
else max = Math.max(max, prices[j++] - prices[i]);
}
return max;
}
```