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

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. Html table 实现Excel多格粘贴

    Html table 实现Excel多格粘贴 电商网站的后台总少不了各种繁杂数据的录入,旁边的运营妹子录完第138条商品的时候,终于忍不住转身吼到:为什么后台的录入表不能像Excel那样多行粘贴!!! ...

  2. HDU 题目分类

    转载自新浪博客,, http://blog.sina.com.cn/s/blog_71ded6bf0100tuya.html 基础题: 1000.1001.1004.1005.1008.1012.10 ...

  3. BOM 之 screen history

    /*    avaiHeight // 屏幕的像素高度减去系统部件高度之后的值    var ah = screen.availHeight;     alert(ah); */    /* avai ...

  4. C# 仿迅雷风格选项卡

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) { listView1.FullRowSelect = ...

  5. Asp.net MVC的Controller激活理解【学习笔记】

    DefaultControllerFactory 是MVC默认的Controller查找和激活工厂类我们可以通过自定义ControllerFactory替换DefaultControllerFacto ...

  6. Coursera《machine learning》--(8)神经网络表述

    本笔记为Coursera在线课程<Machine Learning>中的神经网络章节的笔记. 八.神经网络:表述(Neural Networks: Representation) 本节主要 ...

  7. [BZOJ 2127] happiness 【最小割】

    题目链接:BZOJ - 2127 题目分析 首先,每个人要么学文科,要么学理科,所以可以想到是一个最小割模型. 我们就确定一个人如果和 S 相连就是学文,如果和 T 相连就是学理. 那么我们再来确定建 ...

  8. E: Write error - write (28 No space left on device)

    1:在终端中运行cd命令,提示: e: Write error - write (28 No space left on device) E: Cant mmap an empty file 2:使用 ...

  9. 应用安全技术趋势之 Top 5

    而今,大多数应用都依赖于像入侵防护系统(Instrusion Prevention System)和 Web 应用防火墙(Web Application Firewall,以下全文简称 WAF)这样的 ...

  10. astyle代码格式化

    Artistic Style 1.24 A Free, Fast and Small Automatic Formatterfor C, C++, C#, and Java Source Code 项 ...