The idea is simple, use a loop to record the max profit in i days. At i + 1day, add ith day price in buying list. Then compare the max profit at i + 1 day with previous max.

```
public int maxProfit(int[] prices) {
if(prices == null || prices.length < 2)
return 0;
int low = prices[0], profit = 0;
for(int i = 1; i < prices.length; i++) {
low = Math.min(low, prices[i - 1]);
profit = Math.max(profit, prices[i] - low);
}
return profit;
}
```