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. Windows 10+Ubuntu双系统修复Ubuntu启动引导

    U盘启动,联网 $ sudo su sudo add-apt add-apt-repository ppa:yannubuntu/boot-repair apt-get update apt-get ...

  2. MySQL写delete语句时不支持表别名

    今天写代码时发现了下面一个比较奇怪的问题: 有下面的删除数据的SQL ; 这个sql本来没有问题,但是在MySQL中执行时会报错 ; 原因是 MySQL写delete语句时不支持表别名,困扰了我一会儿 ...

  3. 局域网映射到公网-natapp实现

    在开发时可能会有这样的需求: 需要将自己开发的机器上的应用提供到公网上进行访问,但是并不想通过注册域名.搭建服务器等等一系列繁琐的操作来实现. 例如:微信公众号的开发调试就需要用到域名访问本机项目. ...

  4. java中的final关键字(2013-10-11-163 写的日志迁移

     final关键字:修饰符,表示最后的.最终的    修饰类: 表示该类不能派生子类(不能被继承)   1.当不希望父类的的某个方法被子类覆盖(override)时,可以用final关键字来修饰.   ...

  5. 配置wamp开发环境

    新手在PHP网站建设时,会使用使用PHP的集成开发环境,这样利于开发和理解!但是做为一个网站开发人员,会独立的配置开发环境这是必须的……因为集成的环境毕竟是固定的,不利于自己的开发.好,废话少说咱现在 ...

  6. oracle如何保证读一致性 第一弹

    oracle保证读一致性原理   1:undo segment的概念                   当数据库进行修改的时候,需要把保存到以前的old的数据保存到一个地方,然后进行修改,用于保存o ...

  7. HDU - 4027 Can you answer these queries?(线段树)

    给定一个长度为n的序列,m次操作. 每次操作 可以将一个区间内的所有数字变为它的根号. 可以查询一个区间内所有元素的和. 线段树的初级应用. 如果把一个区间内的元素都改为它的根号的话,是需要每个数字都 ...

  8. 算法导论 第七章 快速排序(python)

    用的最多的排序 平均性能:O(nlogn){随机化nlogn} 原地址排序 稳定性:不稳定 思想:分治 (切分左右) 学习方式:自己在纸上走一遍   def PARTITION(A,p,r): x = ...

  9. Linux学习-Boot Loader: Grub2

    boot loader 的两个 stage 在 BIOS 读完信息后,接下来就是会到第一个开机装置 的 MBR 去读取 boot loader 了.这个 boot loader 可以具有选单功能.直接 ...

  10. 使用docker+tomcat部署jenkins