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


题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/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 k transactions.

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Example 1:

Input: [2,4,1], k = 2
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

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

题目大意

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

解题方法

就是123. Best Time to Buy and Sell Stock III昨天的题,只是把交易2次改成了交易k次。这次题目有个坑,就是给了一个特别大的k,导致构建数组的时候,内存超了。在123题目里也说了,如果k>=N的时候相当于没有限制,题目退化成了不限次数的交易,所以我们直接求今天比昨天高的部分即可。当k<N的时候,我们仍然使用两个变量,全局的收益g和当前天卖出股票的收益l.

以下来自Grandyang的博客

这里我们需要两个递推公式来分别更新两个变量local和global,参见网友Code Ganker的博客,我们其实可以求至少k次交易的最大利润。我们定义local[i][j]为在到达第i天时最多可进行j次交易并且最后一次交易在最后一天卖出的最大利润,此为局部最优。然后我们定义global[i][j]为在到达第i天时最多可进行j次交易的最大利润,此为全局最优。它们的递推式为:

local[i][j] = max(global[i - 1][j - 1] + max(diff, 0), local[i - 1][j] + diff)

global[i][j] = max(local[i][j], global[i - 1][j]),

其中局部最优值是比较前一天并少交易一次的全局最优加上大于0的差值,和前一天的局部最优加上差值后相比,两者之中取较大值,而全局最优比较局部最优和前一天的全局最优。

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

日期

2018 年 12 月 1 日 —— 2018年余额不足了

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

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  2. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    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 188. Best Time to Buy and Sell Stock IV (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 ...

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

  5. 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...

  6. 【LeetCode】Best Time to Buy and Sell Stock IV

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

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

  8. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

  9. [LeetCode][Java] Best Time to Buy and Sell Stock IV

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

随机推荐

  1. php5.6升级7

    1. 检查当前安装的 PHP查看当前 PHP 版本 php -v查看当前 PHP 相关的安装包 yum list installed | grep php2. 更换 RPM 源#Centos 5.X: ...

  2. mysql 计算日期为当年第几季度

    select T21620.日期 as F21634, QUARTER('98-04-01')  as quarter                       #返回日期是一年的第几个季度   - ...

  3. (转载)Java里新建数组及ArrayList java不允许泛型数组

    java中新建数组: String[] s;//定义的时候不需要设置大小 s = new String[5];//为数组分配空间时就要设置大小   对于ArrayList, ArrayList< ...

  4. CPF C#跨平台UI框架发布安卓端预览版

    CPF的安卓端适配采用Xamarin的安卓绑定库,而不是Xamarin.Form.CPF和flutter差不多,完全由skia绘制,基本不依赖原生控件. 当前还只是预览版,不建议用在正式项目中. 可能 ...

  5. 日常Java 2021/10/26

    HashSet基于HashMap来实现的,是一个不允许有重复元素的集合.HashSet 允许有null 值. HashSet是无序的,即不会记录插入的顺序. HashSet不是线程安全的,如果多个线程 ...

  6. 【C/C++】例题5-4 反片语/算法竞赛入门经典/C++与STL入门/映射:map

    本题是映射:map的例题. map:键值对. [题目] 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词. 在判断是否满足条件时,字母不分大小写,但在输出 ...

  7. 【软件安装与配置】【Java】Eclipse For Java EE的安装

    Eclipse For Java EE的安装 哔哩哔哩:萌狼蓝天 微信公众号:萌狼蓝天 博客:萌狼工作室 00 提前说明 因为编程涉及的环境配置资源比较多,所以我专门创建了一个文件夹(Developm ...

  8. Quartz在.NET中的使用

    一.背景 例如需要在某年某月去将数据库的某个数据更新或者同步,又或者是每隔一段时间来执行一部分代码去调用接口,但是又不想人为的手动去执行 针对此类业务可以使用"定时调用任务",市面 ...

  9. DMA(Data Migration Assistant)迁移SQLServer数据库

    DMA适用于 本地SQLServer向Azure SQL Database迁移 两台不同的数据库服务器之间迁移 高版本->低版本 或 低版本->高版本 本文以两台不同服务器的低版本(SQL ...

  10. HashMap的putAll方法介绍说明

    jdk1.8 使用putAll时,新map中的值仅为旧map值所对应对象的引用,并不会产生新对象. 如下,使用for循环赋值! public void putAll(Map<? extends ...