题目链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
想多了,想到dp上去了,这个题就是看差值,例如「1,4,5,7」显然收益最大为6,就是如果今天的价格比明天低就买然后明天卖,因为可以进行多次交易,所以这样贪心收益最高
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if( len < 2 ) {
return 0;
}
int ans = 0;
for(int i = 1; i < len; i++) {
ans += max(0, prices[i] - prices[i -1]);
}
return ans;
}
};