股票问题可简化为: 求一数字序列相连项的非负差值之和
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1) return 0;
int max = 0;
for (int i = 0; i < n - 1 ; i++){
if (prices[i+1] > prices[i]){
max = max + prices[i+1] - prices[i];
}
}
return max;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1) return 0;
int max = 0;
for (int i = 0; i < n - 1 ; i++){
if (prices[i+1] > prices[i]){
max = max + prices[i+1] - prices[i];
}
}
return max;
}
};