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

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))复杂度。然而复杂度太高。 逻辑简单实现简单的解法,当k >= n/2时,按照第二题的做法解即可;当k < n/2时,可能会有合并多个上升曲线的情况,设某一天价格为 cur,而刚好在这一天,有以下两种情况,第k次卖sk,第k次买bk:有以下逻辑sk = max(sk, bk + cur),bk = max(bk, sk-1 - cur),循环下去即可,注意是从后往前以此循环。TC为O(kn)。

PS: 事实上通过差价累加,有O(n)的解法,大致思想是讲两个相隔的上升梯度合并作为一个上升梯度,然后上一个梯度的peak和下一个梯度的valley的差值,作为第二个上升梯度;高度,宽度夹在中间的梯度是不可能在这两个梯度之前被选择的;而这两个梯度被选择之后,是允许选择中间的梯度的。

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的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

    [121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...

  2. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

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

  3. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

  4. [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 al ...

  5. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

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

  6. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

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

  7. [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

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

  8. [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

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

  9. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

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

随机推荐

  1. C++语言体系设计哲学的一些随想(未完待续)

    对于静态类型语言,其本质目标在于恰当地操作数据,得到期望的值.具体而言,需要: (1)定义数据类型 你定义的数据是什么,是整形还是浮点还是字符.该类型的数据可以包含的值的范围是什么. (2)定义操作的 ...

  2. nullptr和NULL 区别

    注:本文内容摘自网络,准确性有待验证,现阶段仅供学习参考.尊重作品作者成果,原文链接 :http://www.2cto.com/kf/201302/190008.html 1.为什要有nullptr ...

  3. WPF DataGrid 行头小三角

    <DataTemplate x:Key="RowHeaderTemplate"> <StackPanel Orientation="Horizontal ...

  4. js更新页面,随机更新数字

    代码1: <script> function getRandom(){ var i = Math.random()*40+160; document.getElementById(&quo ...

  5. php引用(&)详解及注意事项

    php的引用(就是在变量或者函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容. 与C语言中的指针是有差别的.C语言中的指针里面存储的是变量的内容,在内存中存 ...

  6. js输出单一字符字串

    <!DOCTYPE HTML> <html> <body> <input type="text" id="str" & ...

  7. MAC 安装Ruby On Rails

    MAC 安装Ruby On Rails 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发 ...

  8. 学习Swift -- 泛型

    泛型 泛型代码可以让你写出根据自我需求定义.适用于任何类型的,灵活且可重用的函数和类型.它的可以让你避免重复的代码,用一种清晰和抽象的方式来表达代码的意图. 泛型所解决的问题 先来看一个交换两个int ...

  9. 自定义Web控件写事件

    --------------------myRegister1.ascx前台代码----------------------- <script src="js/Jquery1.7.js ...

  10. Kernel Bypass & Offload 介绍

    系统网络优化可以有两方面的工作可以做:1 绕开内核(bypass):2 用硬件替代软件(offload). 具体包括: 1. 绕开内核: 不使用内核内核子系统的功能,采用自己实现的相同功能的代码来处理 ...