题目来源:

  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. iOS 8.0正式公布啦

    从昨晚開始,苹果新版移动操作系统iOS 8.0開始提供免费下载服务.新版操作系统提供有数百个新功能,当中包含相机功能中添加了延时摄影模式,能够使用其它厂商开发的输入法.购买的App应用程序能够与最多6 ...

  2. makefile之cmake入门

    cmake是一款生成makefile的软件:在生成makefile之前,首先是写一个CMakeLists.txt文件: 以下为典型例子: 先看目录tree, 在test文件夹中有:include目录, ...

  3. 队列的实现 -- 数据结构与算法的javascript描述 第五章

    队列也是列表的一种,有不同于列表的规则. 先进先出 入队方法 出队方法 可以找到队首 可以找到队尾 可以查看队列有多长 可以查看队列是否为空 这是一个基本的需求,围绕他来实现,当然我们可以自己扩展列表 ...

  4. 常用Android快速开发框架

    在做项目的过程中遇到了很多困难,于是收集了一些快速开发的框架,使用后大大提高了项目开发速度,无论什么项目都可以使用的到,在此分享给大家,希望能对大家有帮助!(个人建议:有时间的同学可以看一下这些优秀框 ...

  5. codevs 1028 花店橱窗布置 KM算法

    题目链接 n个花, m个花瓶, 每个花放到一个花瓶里会产生一个值w[i][j], 一个花只能放到一个花瓶里, 一个花瓶只能放一个花, 求产生的最大值. 带权二分图模板. #include <io ...

  6. 在python文本编辑器里如何设置Tab为4个空格

    python中缩进一般为四个空格,我总结3种常用编辑器中种如何设置Tab键为四个空格 第一种:下载python3.5时自带de 一个IDLE编辑器 在Options选项下的Configure IDLE ...

  7. sqlplus命令手册

    show errorshow allshow usersqlplus show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看所有系统变量值 SQ ...

  8. html 转 js 字符串

    看到一个牛人的博客  http://riny.net/lab/#tools_html2js 看了下他的代码  挺棒的 所依赖的两个库在这里 https://github.com/Bubblings/l ...

  9. opencv-python 学习笔记1:简单的图片处理

    一.主要函数 1. cv2.imread():读入图片,共两个参数,第一个参数为要读入的图片文件名,第二个参数为如何读取图片,包括cv2.IMREAD_COLOR:读入一副彩色图片:cv2.IMREA ...

  10. ssm框架理解

    SSM框架理解 最近两星期一直在学JavaEE的MVC框架,因为之前学校开的JavaEE课程就一直学的吊儿郎当的,所以现在真正需要掌握就非常手忙脚乱,在此记录下这段时间学习的感悟,如有错误,希望大牛毫 ...