题目来源:

  https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/


题意分析:

  和上题类似,array[i]代表第i天物品价格,如果只能交易2次。问最大利润。


题目思路:

  这是一个动态规划问题。不难想到把整个数组拆成两部分。那么用两个数组First,second分别array[:i]和array[i:]的最大利润。那么答案就等于max(First[i] + second[i + 1])。


代码(python):

  

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
size = len(prices)
if size == 0: return 0
first,second = [0 for i in range(size)],[0 for i in range(size+1)]
minp = prices[0]
for i in range(size):
if prices[i] <= minp:
minp = prices[i];first[i] = first[i - 1]
first[i] = max(prices[i] - minp,first[i-1])
maxp = prices[size-1]
j = size - 1
while j >= 0:
if prices[j] >= maxp:
maxp = prices[j]
second[j] = max(second[j+1],maxp - prices[j])
j -= 1
ans = 0
for i in range(size):
ans = max(ans,first[i]+second[i+1])
return ans

[LeetCode]题解(python):123-Best Time to Buy and Sell Stock III的更多相关文章

  1. LN : leetcode 123 Best Time to Buy and Sell Stock III

    lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...

  2. 【leetcode】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  3. [leetcode]123. 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 ...

  4. 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  5. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. LeetCode OJ 123. 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 ...

  8. 123. Best Time to Buy and Sell Stock III ——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 123. 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 a ...

  10. 123. Best Time to Buy and Sell Stock III (Array; DP)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 前端web应用的组件化(二) 徐飞

    Web应用的组件化(二) https://github.com/xufei/blog/issues/7 管控平台 在上一篇中我们提到了组件化的大致思路,这一篇主要讲述在这么做之后,我们需要哪些外围手段 ...

  2. poj2486 Apple Tree (树形dp)

    题意:有一颗苹果树,树上的u节点上有num[u]个苹果,树根为1号节点,囧king从根开始走,没走到一个节点就把接点上的苹果吃光,问囧king在不超过k步的情况下最多吃多少个苹果. 解题思路:处理出两 ...

  3. MapReduce实现矩阵相乘

    矩阵相乘能够查看百度百科的解释http://baike.baidu.com/view/2455255.htm?fr=aladdin 有a和b两个矩阵 a:                1   2   ...

  4. Brew install for mac

    安装命令例如以下: curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --st ...

  5. Drupal 7 建站学习手记(四):怎样改动Nivo Slider模块的宽高

    背景 Nivo Slider模块默认大小是用的height: 100%, width 100%, 但IE7及下面的浏览器是不支持百分比宽高的, 而我的项目目标用户基本都是使用XP系统,项目需求是必须兼 ...

  6. JavaScript之获取和设置元素属性

    1.与我前面的随笔获取元素的那些方法不同http://www.cnblogs.com/GreenLeaves/p/5689075.html 获取元素属性的方法getAttribute()不属于docu ...

  7. BZOJ 2879: [Noi2012]美食节( 费用流 + 动态加边 )

    倒着做菜..然后考虑为当前的人做菜对后面的人的影响就可以了..要动态加边 --------------------------------------------------------------- ...

  8. BZOJ 1066: [SCOI2007]蜥蜴( 最大流 )

    结点容量..拆点然后随便写 --------------------------------------------------------------- #include<cstdio> ...

  9. jar文件につぃて

    打包jar文件和设置class路径: 查看jar文件内容:

  10. spoj 1557 GSS3 - Can you answer these queries III 线段树

    题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...