- 思路
贪心最难的就是怎么使用贪心,如下例子,这里使用贪心就是有赚就卖,即1买5卖,3买6卖。这样累计起来的赚的就是最多的。
class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 1) {
return 0;
}
int sum = 0;
for (int i = 1; i < prices.length; i++) {
// 如果是下降,则不卖
if (prices[i] - prices[i-1] <= 0) {
continue;
} else {// 如果是上升,则卖
sum += (prices[i] - prices[i-1]);
}
}
return sum;
}
}



