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 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 实现的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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. ...
- 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 ...
随机推荐
- 多个图标图片(雪碧图)使用CSS样式显示
现在的网页中显示很多图标算是常态,发现项目中页面上用到的图标都是单个图标单个文件,用的时候直接往页面上挂,这确实很常态. 如果,网站是挂在外网上,或者网速过低,又大量使用图标的情况下,由于浏览器和服务 ...
- html常用的小技能
在html中有很多常用小技能,记下来,下次直接看自己的,就不用四处找啦! 1.<li>标签去掉点号:list-style-type:none; 去掉前: 去掉后: 2.<li> ...
- 集成Ehcache
提醒 这一小节的是如何在应用层(service或者module或action类)中使用ehcache 准备工作 下载ehcache 你需要一个js文件 请务必阅读下面代码中的注释!! 分情况选 ...
- Linux一键脚本合集vps
首先,想说说一键脚本流行的原因何在? 众所周知的是,Linux 是占据大半壁江山的服务器系统,但在桌面上的占有率可就远不是那么回事儿了,使用和熟悉 Linux 的人远没有 Windows 多,但又因为 ...
- animation写动画
最近,接到项目需求,需要写大量的动画,那么怎么写呢? 动画是使元素从一种样式逐渐变化为另一种样式的效果.可以用百分比来规定变化发生的时间,或用关键词 "from" 和 " ...
- JQuery从服务器端取得数据绑定到dropdownlist(select)中
http://blog.csdn.net/gaofang2009/article/details/5840783 http://www.cnblogs.com/Mac_Hui/archive/2010 ...
- 去除select下拉框默认样式
去除select下拉框默认样式 select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #; /*很关键:将默认的select选 ...
- GPT分区表的备份与恢复
GPT分区表的备份与恢复 keenshoes 2016-01-13 21:02:25 关键词: GPT, Partition, MBR,APPLE, GUID, Protective MBR 对于现 ...
- Everything Be True-freecodecamp算法题目
Everything Be True 1.要求 完善every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回 ...
- Duizi and Shunzi HDU - 6188 (贪心)2017 广西ACM/ICPC
Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...