题目

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.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

代码:Runtime: 175 ms

 class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit_with_k_transactions(self, prices, k):
days = len(prices)
local_max = [[0 for i in range(k+1)] for i in range(days)]
global_max = [[0 for i in range(k+1)] for i in range(days)]
for i in range(1,days):
diff = prices[i] - prices[i-1]
for j in range(1,k+1):
local_max[i][j] = max(local_max[i-1][j]+diff, global_max[i-1][j-1]+max(diff,0))
global_max[i][j] = max(local_max[i][j], global_max[i-1][j])
return global_max[days-1][k] def maxProfit(self, prices):
if prices is None or len(prices)<2:
return 0
return self.maxProfit_with_k_transactions(prices, 2)

思路

不是自己想的,参考这篇博客http://blog.csdn.net/fightforyourdream/article/details/14503469

跟上面博客一样的思路就不重复了,下面是自己的心得体会:

1. 这类题目,终极思路一定是往动态规划上靠,我自己概括为“全局最优 = 当前元素之前的所有元素里面的最优 or 包含当前元素的最优”

2. 这道题的动归的难点在于,只靠一个迭代公式无法完成寻优。

思路如下:

global_max[i][j] = max( global_max[i-1][j], local_max[i][j])

上述的迭代公式思路很清楚:“到第i个元素的全局最优 = 不包含第i个元素的全局最优 or 包含当前元素的局部最优”

但问题来了,local_max[i][j]是啥?没法算啊~

那么,为什么不可以对local_max[i][j]再来一个动态规划求解呢?

于是,有了如下的迭代公式:

local_max[i][j] = max(local_max[i-1][j]+diff, global_max[i-1][j-1]+max(diff,0))

上面的递推公式 把local_max当成寻优目标了,思路还是fellow经典动态规划思路。

但是,有一部分我一开始一直没想通(蓝字部分),按照经典动态规划思路直观来分析,就应该是local_max[i-1][j]啊,怎么还多出来一个diff呢?

===========================================================================================================

时隔几天再想想,求解local_max[i][j]的过程其实并不能算传统动态规划的思路,之前的思路有些偏差。原因是local_max本身就不是一个“全局”最优,因为计算local[i][j]的时候就已经把最近的一个元素算进去了。local_max[i][j] = max(local_max[i-1][j]+diff, global_max[i-1][j-1]+max(diff,0))这个公式的得来,也真心是原作者巧妙分析的结果,一下就解决了求解N次交易最优的问题。只能膜拜并记住这部分代码。

leetcode 【 Best Time to Buy and Sell Stock III 】python 实现的更多相关文章

  1. [leetcode]Best Time to Buy and Sell Stock III @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...

  2. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

  3. [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 ...

  4. [LeetCode] Best Time to Buy and Sell Stock III

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

  5. LeetCode: Best Time to Buy and Sell Stock III [123]

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

  6. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

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

  7. [leetcode]Best Time to Buy and Sell Stock II @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...

  8. leetcode -- Best Time to Buy and Sell Stock III TODO

    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——Best Time to Buy and Sell Stock III

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

  10. LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)

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

随机推荐

  1. NC57访问报错:java.sql.SQLException: Io 异常: Got minus one from a read call

    一.报错信息 1.  前端登录界面 2.  后台应用日志 报错信息一致为: $$callid= $$thread=[Service Monitor and Runtime Enroment] $$ho ...

  2. 【Java/Android性能优5】 Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强

    本文转自:http://www.trinea.cn/android/android-imagecache/ 主要介绍一个支持图片自动预取.支持多种缓存算法.支持二级缓存.支持数据保存和恢复的图片缓存的 ...

  3. Eucalyptus-利用镜像启动一个Centos实例

    1.前言 使用kvm制作Eucalyptus镜像(Centos6.5为例)——http://www.cnblogs.com/gis-luq/p/3990795.html 上一篇我们讲述了如何利用kvm ...

  4. php编译安装过程中遇到问题

    编译安装PHP时遇到的问题 问题1: configure: error: xml2-config not found. Please check your libxml2 installation. ...

  5. IOS 控件器的创建方式(ViewController)

    ● 控制器常见的创建方式有以下几种 ➢ 通过storyboard创建 ➢ 直接创建 NJViewController *nj = [[NJViewController alloc] init]; ➢ ...

  6. 【HDU4507】恨7不成妻(数位DP)

    点此看题面 大致题意: 让你求出一段区间内与\(7\)无关的数的平方和.与\(7\)无关的数指整数中任意一位不为\(7\).整数的每一位加起来的和不是\(7\)的整数倍.这个整数不是\(7\)的倍数. ...

  7. 2017.12.17 servlet 生命周期

    servlet生命周期一般分为4个: 加载----实例化------服务-----销毁 (1)加载: 加载一般是在运行tomcat容器时来完成,将servlet类加载到tomcat中,或者是客户端发来 ...

  8. Android驱动开发5-7总结

    Android深度探索5-7章总结 介绍了S3C6410开发板的功能,开发板的不同主要是在烧录嵌入式系统的方式不同,以及如何在此开发板上安装Android.紧接着学到介绍到如何在多种平台,使用多种方式 ...

  9. django-redis缓存记录

    对于站点缓存,我们使用redis这款key-value数据库.Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径.Redis的数据类型都是基于基本数据结构的 ...

  10. 【前端_js】js中数字字符串之间的比较

    js中字符串间的比较是按照位次优先,比较各字符的ASCII大小,包括数字字符串之间的比较. 1.console.log("1"<"3");//true 2 ...