题目:
我的解法:动态规划
class Solution {
public int maxProfit(int[] prices) {
int d = prices.length;
int buy = -prices[0], nobuy = 0;
for(int i=1; i<d; i++){
nobuy = Math.max(nobuy, buy + prices[i]);
buy = Math.max(buy, nobuy - prices[i]);
}
return Math.max(buy, nobuy);
}
}
官方题解: