原题地址: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. COJ1013 WZJ的数据结构(十三)

    WZJ的数据结构(十三) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个节点的有根树(根节点为1),每个节点有权 ...

  2. [POJ1144]Network

    来源:Central Europe 1996 思路:Tarjan求割点. 一个点$x$为割点当且仅当: 1.$x$为根结点且有两棵不相交的子树. 2.$x$不为根结点且它的子树中没有可以返回到$x$的 ...

  3. 【BZOJ】1854: [Scoi2010]游戏【二分图】

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 6759  Solved: 2812[Submit][Status] ...

  4. Codeforces 986D Perfect Encoding FFT

    题意: 给定一个数n,选出m个数使得 $\Pi_{i=1}^m a_i\ge n$,求$\sum_{i=1}^m a_i$的最小值 ,其中$m$的大小不限 $n$的长度$\le 10^6$ 简单的计算 ...

  5. HDU 5745 La Vie en rose 暴力

    La Vie en rose 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5745 Description Professor Zhang woul ...

  6. Ubuntu下实现软路由(转)

    参考:http://www.openwrt.pro/post-292.html 个人看法: 1.实现路由在Linux下必须要用到iptables进行转发,这才是路由核心. 2.我觉得对于Linux来说 ...

  7. MikroTik RouterOS网址资源收集

    routeros|mikrotik|ros|软路由论坛|中国路由网|软件路由|软件路由器|routeros技术论坛|路由论坛 - Powered by Discuz!   Mikrotik RB450 ...

  8. ARM Cortex Design Considerations for Debug

    JTAG was the traditional mechanism for debug connections for ARM7/9 parts, but with the Cortex-M fam ...

  9. 十步轻松搞定IIS+PHP环境搭建

    突然心血来潮想着自己一直使用Apache+php的模式,想要了解一下IIS+php的模式.说起来也算是九曲十八弯吧! 第一部分:以ISAPI.dll 扩展的形式 结果按照资料上面说的我就是找不到一个i ...

  10. 查看内核页表kernel_page_tables (aarch32)

    作者 彭东林 pengdonglin137@163.com   平台 Linux-4.10.17 Qemu + vexpress-ca9     概述 通过配置内核,会在/sys/kernel/deb ...