首页 > 其他 > 详细

LeetCode\EPI "Best Time to Buy and Sell Stock"

时间:2014-08-08 15:53:26      阅读:371      评论:0      收藏:0      [点我收藏+]

Also the very first problem on EPI. 

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);
        int prof1 = maxProfit(v1);
        vector<int> v2; v2.assign(prices.begin() + len1, prices.end());
        int prof2 = maxProfit(v2);
        auto i1 = std::min_element(v1.begin(), v1.end());
        int min1 = *i1;
        auto i2 = std::max_element(v2.begin(), v2.end());
        int max1 = *i2;
        int prof3 = max1 - min1;
        return std::max(std::max(prof1, prof2), prof3);
    }
};

 

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

LeetCode\EPI "Best Time to Buy and Sell Stock"

原文:http://www.cnblogs.com/tonix/p/3899268.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!