[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 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).
解题思路:只允许做两次交易,这道题就比前两道要难多了。解法很巧妙,有点动态规划的意思:开辟两个数组f1和f2,f1[i]表示在price[i]之前进行一次交易所获得的最大利润,f2[i]表示在price[i]之后进行一次交易所获得的最大利润。则f1[i]+f2[i]的最大值就是所要求的最大值,而f1[i]和f2[i]的计算就需要动态规划了,看代码不难理解。
代码:
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
length=len(prices)
if length==0: return 0
f1=[0 for i in range(length)]
f2=[0 for i in range(length)] minV=prices[0]; f1[0]=0
for i in range(1,length):
minV=min(minV, prices[i])
f1[i]=max(f1[i-1],prices[i]-minV) maxV=prices[length-1]; f2[length-1]=0
for i in range(length-2,-1,-1):
maxV=max(maxV,prices[i])
f2[i]=max(f2[i+1],maxV-prices[i]) res=0
for i in range(length):
if f1[i]+f2[i]>res: res=f1[i]+f2[i]
return res
[leetcode]Best Time to Buy and Sell Stock III @ Python的更多相关文章
- 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 ...
随机推荐
- JavaScript 的装饰器:它们是什么及如何使用
请访问我的独立博客地址:https://imsense.site/2017/06/js-decorator/ 装饰器的流行应该感谢在Angular 2+中使用,在Angular中,装饰器因TypeSc ...
- Codeforces 666E Forensic Examination SAM+权值线段树
第一次做这种$SAM$带权值线段树合并的题 然而$zjq$神犇看完题一顿狂码就做出来了 $Orz$ 首先把所有串当成一个串建$SAM$ 我们对$SAM$上每个点 建一棵权值线段树 每个叶子节点表示一个 ...
- OpenJ_POJ C16D Extracurricular Sports 打表找规律
Extracurricular Sports 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/D Description As w ...
- java判断集合是否重复的一种便捷方法
内容来自其它网站,感谢原作者! import java.util.ArrayList; import java.util.HashSet; import java.util.List; /** * 通 ...
- JNI之String类型
JNI使用的是改良的UTF-8格式的Strings. 以下文档来自官方: Modified UTF-8 Strings The JNI uses modified UTF-8 strings to r ...
- process information unavailable 的解决办法
有时候在centos上查看java进程时,会遇到process information unavailable 的情况,如下图: 不同账号之间kill进程时,可能会造成这种现象(比如:deploy用户 ...
- Jedis使用总结【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】(转)
前段时间细节的了解了Jedis的使用,Jedis是redis的java版本的客户端实现.本文做个总结,主要分享如下内容: [pipeline][分布式的id生成器][分布式锁[watch][multi ...
- js文件改变之后浏览器缓存问题怎么解决?
升级了js文件,很多页面都引用了这个文件,需要主动清除浏览器缓存才会生效,有没有什么办法可以不主动清除就可以? 修改文件名,加上版本号,或 xxx.js?v=0.101
- Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包
Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...
- Java String练习题及答案
1. 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕 /** * 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕 */ publ ...