[经典] Best Time to Buy and Sell Stock

Posted 小尾巴君

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[经典] Best Time to Buy and Sell Stock相关的知识,希望对你有一定的参考价值。

这一系列求最优值的问题变种挺多

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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

最简单的一个问题,有这样一种情景:我是从未来穿越回来的,已知了一个商品在后面N天的价格,限制是只能买卖一次,那我如何利用穿越回来的优势用一次机会来赚最大的一笔钱。

简单暴力做法,TC为O(n^2);假设全局最大利润为gmax,通过利用min来记录前面已知的最小值,再用当天i值减去min,就是在i天售出能获得的最大利益。整个过程需要更新min O(n)次,更新最大利润gmax O(n)次,总TC为O(n)。

 

2. 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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

类似的情景,但有一些限制,限制是能买卖多次,但是手头不能有多于一个的存货。也就是说手上只能有一只股的炒股问题。

简单暴力做法,TC为O(2^n);但如果真有这样的场景,我们都会选择高点清仓,低点抄底,所以用gmax记录最大利润,用min记录每一个极小值,用max记录下一个极大值,gmax += max - min更新。整个过程,每天更新极小值或者极大值一次,共O(n)次,更新gmax,最坏情况O(n)次。所以总TC为O(n)。

 

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.

限制更中庸,情况更一般,最多两次交易。即找两个和最大的涨曲线。

简单暴力做法,选取4天出来,TC为O(N^4);可以从第1题出发考虑,我们能得到从第0天到第i天中一次能获得的最大利益;那么将数组翻转过来,也能得到从第N天到N-i天中一次能得到的最大利益;再从0到N遍历一遍,找到前后累加值最大的利益,i与N-i无缝连接也保证了可能只进行一次交易的情况。

 

4. 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 k transactions.

这次基本等于没限制了,情况最一般。按照第3题的思路,我们能想到的做法是O(n^2 + n^(k-1))复杂度。然而复杂度太高。假设有m个(valley,peak)对,m < k 时,选出m个对即可;但是m >= k时,先选取前k大的对,累加和gmax,然后对k个对中间的k-1个间隙从小到大排列,如果第k+1大的对比间隙最小值大,则gmax += diff,直到k+i大的数比间隙弹出来的值小,或者k+i == m了,则停止算法。

 

5. 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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

相对于第2题,多了一个cooldown,DP遇到这种问题,通常解法是每个节点多一个记录位,来抵消cooldown带来的后续影响。下降趋势时,找到最低点作为买入价格,上升时到最高点有两个选择,卖或者不卖;不卖是因为下一天可能会有大幅度下跌,可以将买入价格调整在那时候;opt[i][1]代表第i天卖的总收益,opt[i][0]代表第i天没卖的总收益。

以上是关于[经典] Best Time to Buy and Sell Stock的主要内容,如果未能解决你的问题,请参考以下文章

最强解析面试题:best-time-to-buy-and-sell-stock

算法分析与设计第一周121.Best Time to Buy and Sell Stock&122. Best Time to Buy and Sell Stock II

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

121. Best Time to Buy and Sell Stock

122. Best Time to Buy and Sell Stock II

123. Best Time to Buy and Sell Stock III***