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 ...
随机推荐
- Oracle SQL Developer-3.2.20.09.87 Windows 10启动问题处理&配置
用了好多年的工具,准备在笔记本上使用时启动不了,但在办公室PC上可以正常使用.两者电脑OS都一样,一个是全新安装.一个是从Windows 7升级而来.下载了最新版发现版本到17了,Oracle刷版本号 ...
- Angular ui-route介绍
参考博客: https://www.cnblogs.com/haogj/p/4885928.html 原文地址:http://www.ng-newsletter.com/posts/angular-u ...
- app后台管理系统框架metronic的学习笔记
先来看效果: 给出当前页面的代码: <!DOCTYPE html> <!--[if IE 8]> <html lang="zh" class=&quo ...
- Redis基础对象
Redis 中每个对象都由一个 redisObject 结构表示 typedef struct redisObject { //类型 unsigned type:; //编码 unsigned enc ...
- 工作流性能优化(敢问activiti有扩展性?)(1)
工作流待办(首页待办列表),加载缓慢,activiti本机,看了代码又是全部数据加载到内存,然后代码过滤,我为什么又说又呢? 用VisualVM做性能测试: 之前同事给的解决方案: 1.把&quo ...
- World Wind Java开发之五——读取本地shp文件(转)
http://blog.csdn.net/giser_whu/article/details/41484433 World Wind Java 使用IconLayer图层类表现点和多点数据,使用Ren ...
- hdu-1598 find the most comfortable road---kruskal+枚举下界
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Ro ...
- Exceptions and Errors on iOS
异常:程序缺陷导致:不可恢复:给开发者使用: 错误:资源受限导致:可恢复:提示给用户. https://blog.jayway.com/2010/10/13/exceptions-and-errors ...
- tomcat的启动和部署
方法/步骤 1 一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-windows-i586.exe可执行程序,默认安装即可. 备注:路径可以其他盘符,不建议路径包含中文名及特殊符号. ...
- 【算法】Fibonacci(斐波那契数列)相关问题
一.列出Fibonacci数列的前N个数 using System; using System.Collections.Generic; using System.Linq; using System ...