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;
          
    }
};

 

posted @ 2014-11-25 10:22  dupuleng  阅读(86)  评论(0)    收藏  举报