原题地址: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的更多相关文章

  1. 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 ...

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

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

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

  4. 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 ...

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

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

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

  8. 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. ...

  9. 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. IO写 PrintWriter

    private static final String FILENAME = "c:\\temp\\out.txt"; PrintWriter pw = null; try { p ...

  2. 关于IEnumerator<T>泛型枚举器 和 IEnumerable<T>

    在开发中我们经常会用到 IEnumerable<T> xxx 或者 List<T> xxx 这种集合或者集合接口,实际上就是一个线性表嘛然后结合C#提供的语法糖 foreach ...

  3. ARP协议详解之ARP动态与静态条目的生命周期

    ARP协议详解之ARP动态与静态条目的生命周期 ARP动态条目的生命周期 动态条目随时间推移自动添加和删除. q  每个动态ARP缓存条目默认的生命周期是两分钟.当超过两分钟,该条目会被删掉.所以,生 ...

  4. Uncaught Error: Syntax error, unrecognized expression: [flag=]报错处理方法

    今早运行项目的时候报这个错误: 百度翻译的解释是:未命名错误:语法错误,未识别表达式:[FLAG= ]   也就是书写规范问题. 可是我查了对应的js: 字符串拼接没什么问题,经常这样写. 这时看报错 ...

  5. django-模板初探

    一般而言,我们在视图函数中处理各种业务逻辑之后,应该返回一个 HttpResponse 对象.而 HttpResponse 对象的第一个参数接受字符串或者是迭代器,作为响应报文的主体.但是这意味着我们 ...

  6. NOIp模拟赛 巨神兵(状压DP 容斥)

    \(Description\) 给定\(n\)个点\(m\)条边的有向图,求有多少个边集的子集,构成的图没有环. \(n\leq17\). \(Solution\) 问题也等价于,用不同的边集构造DA ...

  7. 2015 年度新增开源软件排名 TOP 100 - 开源中国社区

    2015 年度新增开源软件排名 TOP 100 - 开源中国社区 39.ABTestingGateway http://www.oschina.net/news/69808/2015-annual-r ...

  8. Java嵌入式数据库H2学习总结(三)——在Web应用中嵌入H2数据库

    H2作为一个嵌入型的数据库,它最大的好处就是可以嵌入到我们的Web应用中,和我们的Web应用绑定在一起,成为我们Web应用的一部分.下面来演示一下如何将H2数据库嵌入到我们的Web应用中. 一.搭建测 ...

  9. ​0​天​掌​握​i​O​S​开​发​之​D​a​y​2​ ​-​ ​内​存​管​理 (给学生讲解的课件,总结的不错)

    from:   10​天​掌​握​i​O​S​开​发​之​D​a​y​2​ ​-​ ​内​存​管​理

  10. UINavigationController 、UINavigationBar 、UINavigationItem 超清晰直观详解(扩展)

    ios开发中如何隐藏各种bar 状态条Status Bar [UIApplication sharedApplication].statusBarHidden = YES; 或者 // iOS3.2+ ...