Best Time to Buy and Sell Stock III

原创 2015年07月10日 22:05:40

leetcode中的股票问题(3)

来看看系列第三题咯

原题

Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
翻译:同样,一个表示每天价格的数组,同样要获得最大的收益,但是最多只允许你进行两手交易哟~

问题分析

相比于最多进行一手交易,这个问题就有点抽象啦,还是先上图:
股票趋势图
我们直观地猜一猜,大概也能猜出个答案吧~
股票趋势图
感觉得到,但是怎样去理解这两手交易呢?我的第一想法也是“分而治之”——对于某个坐标,左边做多允许一手,右边也是最多允许一手,这样一来,不就转化成两个“Best Time to Buy and Sell Stock I”中的子问题了吗?
但是问题来了,对于坐标i,其左边最多允许一手,按照“Best Time to Buy and Sell Stock I”中的方法,在O(n)的时间内可以做到,从左往右扫一遍即可,但是其右边的收益却要每次从i开始扫到末尾,复杂度是O(n2),是都不用试,肯定通不过大数据测试。
去Discuss上参考了别人的想法,发现从右往左扫可以解决这个问题,的确,我只要记住从右到目前的最大值,然后只要现价足够低,就更新最大收益的标记,基本和从左往右扫是相反的过程。
然后就简单啦,对于所有的i,把左右最大收益相加,取其中最大的就OK啦~

代码

public int maxProfit(int[] prices) {

    if(prices.length < 2)
    {
        return 0;
    }
    //left to right scan
    int[] left = new int[prices.length];
    //right to left scan
    int[] right = new int[prices.length];

    //for index i, find the max profit can be made
    //if at most one transaction can be completed before day i
    int minTag = prices[0];
    left[0] = 0;
    for(int i = 1; i < prices.length - 1; i++)
    {
        left[i] = Math.max(left[i - 1], prices[i] - minTag);
        minTag = Math.min(minTag,prices[i]);
    }

    //for index i, find the max profit can be made
    //if at most one transaction can be completed after day i
    int maxTag = prices[prices.length - 1];
    right[prices.length - 1] = 0;
    for(int i = prices.length - 2; i >= 0; i--)
    {
        right[i] = Math.max(right[i + 1], maxTag - prices[i]);
        maxTag = Math.max(maxTag,prices[i]);
    }

    int profit = 0;
    for(int i = 0; i < prices.length; i++)
    {
        //find the maximun profit
        if(left[i] + right[i] > profit)
        {
            profit = left[i] + right[i];
        }
    }
    return profit;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

LeetCode Best Time to Buy and Sell Stock III && Best Time to Buy and Sell Stock IV

LeetCode这个买股票题,个人认为是出的比较好的,一共四个问题,其中3和4都是hard,在我看来,4的难度比3还是难了不少,因为他涉及到了dp的优化 这里就稍微再提一下第一问和第二问,第一问已经...

[Leetcode]123.Best Time to Buy and Sell Stock III

问题描述: Say you have an array for which the ith element is the price of a given stock on day i. ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

《leetCode》:Best Time to Buy and Sell Stock III

题目Say you have an array for which the ith element is the price of a given stock on day i.Design an a...

Best time to Buy and Sell Stock III

最多进行两次买卖股票,并且不能再卖出前继续买,所以两次交易必须是分布在2个区间内 我们可以划分2个区间为[0,i]和[i,prices.length-1],注意这样就把原题转换成了和Best tim...

LeetCode123—Best Time to Buy and Sell Stock III

LeetCode123—Best Time to Buy and Sell Stock III

[LeetCode10]Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an...

LeetCode | Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an a...

leetcode Best Time to Buy and Sell Stock III

题目Say you have an array for which the ith element is the price of a given stock on day i.Design an a...

[leetcode] 123.Best Time to Buy and Sell Stock III

题目: My Submissions Question Solution Say you have an array for which the ith element is the price...

Best Time to Buy and Sell Stock I && II && III

Best time to Buy and Sell Stock
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)