lastP记录目前为止看到的最低开仓价格
只要第i天的价格比lastP高,就获利了结
import java.util.*;
public class Solution {
public int maxProfit (int[] prices) {
if (prices.length <= 1) return 0;
int lastP = Integer.MAX_VALUE;
int profit = 0;
for (int p : prices) {
if (p > lastP)
profit += p - lastP;
lastP = p;
}
return profit;
}
}