Best Time to Buy and Sell Stock II
/* one can sell and buy at the same day greedy algorithm: just find the increase subsequence */ class Solution { public: int maxProfit(vector<int> &prices) { int sumProfit = 0; if(prices.size() == 0 || prices.size() == 1 ) return 0; for(int i = 0; i < prices.size()-1; ++i) { if(prices[i+1]-prices[i] > 0) sumProfit += prices[i+1]-prices[i]; } return sumProfit; } };

浙公网安备 33010602011771号