贪心算法,有利可图即买入,卖出。
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = prices.size();
if(len < 2) return 0;
int ans = 0;
for(int i = 1; i < len; i ++)
{
if(prices[i] > prices[i-1])
ans += (prices[i] - prices[i-1]);
}
return ans;
}
};