题目:
思路:
只要后面比前面大,那就卖就完事了.
贪心的思想. 这道题应该是可以动态规划的.但是贪心简单得多.
代码是:
//code
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if (len < 2) {
return 0;
}
int res = 0;
for (int i = 1; i < len; i++) {
int diff = prices[i] - prices[i - 1];
if (diff > 0) {
res += diff;
}
}
return res;
}
};