作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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.

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 (i.e., you must sell the stock before you buy again).

Example 1:

Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.

Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

题目大意

给出了一堆股票价格,最多做两次交易,求最大的收益。

解题方法

这个题太难了,看了一个多小时没看懂。直接抄的Gradyang的做法,罪过罪过。

地址:http://www.cnblogs.com/grandyang/p/4281975.html

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices: return 0
N = len(prices)
g = [[0] * 3 for _ in range(N)]
l = [[0] * 3 for _ in range(N)]
for i in range(1, N):
diff = prices[i] - prices[i - 1]
for j in range(1, 3):
l[i][j] = max(g[i - 1][j - 1] + max(diff, 0), l[i - 1][j] + diff)
g[i][j] = max(l[i][j], g[i - 1][j])
return g[-1][-1]

日期

2018 年 11 月 29 日 —— 时不我待

【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)的更多相关文章

  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 买卖股票的最佳时间 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]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: 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 ...

  5. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

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

    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 123. Best Time to Buy and Sell Stock III (stock problem)

    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#123 Best Time to Buy and Sell Stock III

    原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...

  9. LeetCode 122 Best Time to Buy and Sell Stock II 解题报告

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

随机推荐

  1. R语言因子排序

    画图的时候,排序是个很重要的技巧,比如有时候会看下基因组每条染色体上的SNP的标记数量,这个时候直接做条形图是一种比较直观的方法,下面我们结合实际例子来看下: 在R环境下之际构建一个数据框,一列染色体 ...

  2. chown & chmod用法

    chown & chmod 1. chown更改文件的属主&属组 NAME chown - 改变文件的属主和属组(change file owner and group) 用法 cho ...

  3. kubernetes部署kube-controller-manager服务

    本文档介绍部署高可用 kube-controller-manager 集群的步骤. 该集群包含 3 个节点,启动后将通过竞争选举机制产生一个 leader 节点,其它节点为阻塞状态.当 leader ...

  4. orcale => 含义

    => 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.ps(说实话,就是Oracle再执行存储过程中,类似于在word中进行替换一样的感觉,比如说你默认的情况下是你定义了默认参 ...

  5. 虚拟节点轻松应对 LOL S11 百万并发流量——腾竞体育的弹性容器实践

    作者 刘如梦,腾竞体育研发工程师,擅长高并发.微服务治理.DevOps,主要负责电竞服务平台架构设计和基础设施建设. 詹雪娇,腾讯云弹性容器服务EKS产品经理,主要负责 EKS 虚拟节点.容器实例相关 ...

  6. go 函数进阶

    目录 回调函数和闭包 高阶函数示例 回调函数(sort.SliceStable) 闭包 最佳闭包实例 回调函数和闭包 当函数具备以下两种特性的时候,就可以称之为高阶函数(high order func ...

  7. addict, address, adequate.四级

    addict addiction – a biopsychosocial [生物社会心理学的 bio-psycho-social] disorder characterized by persiste ...

  8. LeetCode382-链表随机节点

    原题链接:[382. 链表随机节点]:https://leetcode-cn.com/problems/linked-list-random-node/ 题目描述: 给定一个单链表,随机选择链表的一个 ...

  9. ebs 初始化登陆

    BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...

  10. shell脚本统计多个CPU利用率

    本节主要内容:top命令统计CPU的利用率 一,问题分析 MySQL在Linux下是多线程的,而且只能将多个线程分布到一个CPU上.因此,使用小型服务器,或者PC SERVER,多个CPU利用率并不高 ...