贪心算法
1.寻找递增区间,将所有的递增区间的最大和最小值之差相加
class Solution {
public:
int maxProfit(vector<int> &prices) {
vector<int>::iterator p;
int low, isfirst = 1, max = 0;
for(p = prices.begin(); p != prices.end(); p ++){
if((p + 1) == prices.end()){
if(!isfirst){
max += *p - low;
}
break;
}else{
if(*p < *(p + 1)){
if(isfirst){
low = *p;
isfirst = 0;
}
}else if(*p > *(p + 1)){
if(!isfirst){
max += *p - low;
isfirst = 1;
}
}
}
}
return max;
}
};