Best Time to Buy and Sell Stock II

 

    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(prices.empty())
            return 0;
        int buy = prices[0];
        int maxprofit = 0;
        for(int i=1;i<prices.size();i++)
        {
            if(prices[i]>buy)
                maxprofit += (prices[i]-buy);
            buy = prices[i];
        }
        
        return maxprofit;

        
    }

  

posted @ 2013-10-08 15:25  summer_zhou  阅读(118)  评论(0编辑  收藏  举报