http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

这三道题,很好的进阶。1题简单处理,2题使用贪心,3题使用动态规划。

话说这叫一维动态规划,嗯。又复习了《算法导论》中和贪心以及动态规划有关的知识,记录如下:

动态规划的标志:最优子结构、子问题重叠。

1.找最优子结构

      2.定义递归公示(列一个式子出来,并定义好这个式子到底是什么意思)。

      3.写自底向上或递归备忘录法。

比如本问题:f(i,j) = max{f(i,k)+f(k,j)}           其中:f(i,j)表示从i到j的所有数,进行一次交易能获得的最多的钱数。最终要求的是f(0,n-1),k从1到n-2.

再深化一下:

fp(i,j)表示从i 到 j 进行最多进行 p 次交易可以获得的钱数,则:

fp(i,j) = max {fp-1(i,j), fp-p'(i,k)+fp'(k,j)}     其中:p'从1到p-1,k从i+1到j-1.

这大概叫二维动态规划(或许)。

贪心算法是从局部最优,能够做到全局最优。可分成一步步的,当前的选择受之前做过的选择的影响。而动态规划,是受后面要做的选择的影响。

于是有了下面代码,复杂度O(n2)然后超时了。

class Solution {
public:
//start 第一个元素,end 最后一个元素的下标
int onceBuyAndSell(vector<int> &prices,int start,int end)
{
int ans = ;
int max = prices[end];
for(int i = end - ;i>=start;i--)
{
if(max - prices[i]>ans)
ans = max - prices[i];
if(prices[i]>max)
max = prices[i];
}
return ans;
} int maxProfit(vector<int> &prices) {
if(prices.empty())
return NULL;
if(prices.size()==)
return ; int ans = onceBuyAndSell(prices,,prices.size()-); if(prices.size()< || ans == )
return ans; int temp = ;
for(int itor = ;itor<prices.size()-;itor++)
{
temp = onceBuyAndSell(prices,,itor) + onceBuyAndSell(prices,itor+,prices.size()-);
if(temp> ans)
ans = temp;
}
return ans;
}
};

在网上找资料,参考了http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-and-sell-stock-iii%EF%BC%8C%E4%B8%80%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/

使用了动态规划中打表的方法,降低了复杂度O(n).代码如下:

class Solution {
public:
vector<int> ansN;

//ansN[i]表示,从i到最后的所有元素,只进行一次交易的话,可以得到的最多值。
void onceBuyAndSell(vector<int> &prices,int start,int end)
{
int ans = ;
int max = prices[end];
ansN[end] = ;
for(int i = end - ;i>=start;i--)
{
if(max - prices[i]>ans)
ans = max - prices[i];
if(prices[i]>max)
max = prices[i];
ansN[i] = ans;
}
} int maxProfit(vector<int> &prices) {
if(prices.empty())
return NULL;
if(prices.size()==)
return ; ansN.resize(prices.size());
//先来一遍从后面计算的。
onceBuyAndSell(prices,,prices.size()-); if(prices.size()< )
return ansN[]; int min = prices[];
int ans2 = ; int ans = ansN[]; for(int itor = ;itor<prices.size()-;itor++)
{
if(prices[itor] - min>ans2)
ans2 = prices[itor] - min;
if(prices[itor]<min)
min = prices[itor]; if(ans2 + ansN[itor+]> ans)
ans = ans2 + ansN[itor+];
}
return ans;
}
};

LeetCode OJ--Best Time to Buy and Sell Stock III的更多相关文章

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

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

  2. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  3. [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. Design an al ...

  4. LN : leetcode 123 Best Time to Buy and Sell Stock III

    lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...

  5. [LeetCOde][Java] 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 a ...

  6. [LeetCode OJ] Best Time to Buy and Sell Stock I

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

  7. LeetCode OJ - Best Time to Buy and Sell Stock

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...

  8. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

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

  9. leetcode 123. Best Time to Buy and Sell Stock III ----- java

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

  10. leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

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

随机推荐

  1. (70)zabbix telnet监控类型

    概述 zabbix监控的方式很多,例如前面讲到的agent.snmp以及后续后续要讲到ssh和今天要讲到的telnet.流程很简单,创建item-->配置ip.用户.密码.端口.脚本->z ...

  2. Python PyAudio 安装使用

    Python PyAudio安装: Python3.7 无法安装pyaudio pip install pyaudio提示error: Microsoft Visual C++ 14.0 is req ...

  3. python 中 * 与**的使用

    1.参数中使用如果是函数定义中参数前的*表示的是将调用时的多个参数放入元组中,**则表示将调用函数时的关键字参数放入一个字典中 如定义以下函数 def func(*args): print(args) ...

  4. JAVA基础篇—文件与流

    处理字节流的抽象类 InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等. OutputStream是字节输出流的所有类的超类,一般我们使用它的 ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. POJ:1094-Sorting It All Out(拓扑排序经典题型)

    Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Description An ascending sorted sequence ...

  7. 并查集:CDOJ1594-老司机的奇幻漂流 (食物链)

    老司机的奇幻漂流 UESTC - 1594 Problem Description 老司机在救出了女票之后,就和她在全世界旅游,有一天,他们来到了一个神奇的小岛上. 这个小岛上有三种动物,他们互相克制 ...

  8. 给vagrant中的precise64升级VBoxGuestAdditions

    位置:/usr/share/virtualbox/VBoxGuestAdditions.iso 在host(ubuntu 12.04 64)中: 查看虚拟机的名字:jb@H38:~/vm/vagran ...

  9. edge浏览器识别ip地址为手机号的解决办法

    edge浏览器识别ip地址为手机号的解决办法 今天突然发现类似101.231.70.242的ip地址会在edge浏览器里面识别为可点击的链接,后来看了一下,原因就是被识别为手机号了,因为我发现点击的时 ...

  10. jenkins 之 iOS 打包及上传至蒲公英

    准备条件 iMAC(要 Mac OS 系统,安卓 和 苹果 可以在同一台电脑上打包) xcode 最新版,要已安装对应的开发证书(生成一个 Ad-Hoc 类型的包就有了) brew(当前管理员账户安装 ...