码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Best Time to Buy and Sell Stock II"

时间:2014-08-08 15:58:06      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   cti   div   amp   

No limit to transaction count, so it is a recursive sum calculation. Take care of boundary condition.

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        size_t len = prices.size();
        if (len <= 1) return 0;
        else if (len == 2)
        {
            if (prices[0] >= prices[1]) return 0;
            else return prices[1] - prices[0];
        }
        int len1 = len / 2;
        int len2 = len - len1;

        vector<int> v1; v1.assign(prices.begin(), prices.begin() + len1 + 1);
        int prof1 = maxProfit(v1);
        vector<int> v2; v2.assign(prices.begin() + len1, prices.end());
        int prof2 = maxProfit(v2);        
        return prof1 + prof2;
    }
};

 

LeetCode "Best Time to Buy and Sell Stock II",布布扣,bubuko.com

LeetCode "Best Time to Buy and Sell Stock II"

标签:style   blog   color   io   ar   cti   div   amp   

原文地址:http://www.cnblogs.com/tonix/p/3899300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!