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

【题意】

给定n天股市的票价,最多交易k次,问最大盈利是多少,手上不能同时持有两支股票。

【思路】

考虑加入当天买当天卖这种情况,问题转换为n天总共交易k次(不是最多),因为后一次的买一定在前一次卖之后,所以需要dp[i][j]表示前i天交易j次且最后一次交易在第i天的最大利润。则

dp[i][j]=max(a[i]-a[k]+global[k-1][j-1]) 1<=k<=i

k=i表示当天买当天卖,global[i][j]表示max{dp[k][j]} 1<=k<=i

注意这里是global[k-1][j-1]而不是global[k][j],因为若最后一次交易恰好在第k天,则a[i]-a[k]+global[k][j-1]一共进行j-1次交易,而不是j次。

dp[i][j]=max(a[i]-a[k]+global[k-1][j-1])(1<=k<=i)

=max(a[i]-a[i-1]+a[i-1]-a[k]+global[k-1][j-1],global[i-1][j-1])) (1<=k<=i-1)

=max(a[i]-a[i-1]+dp[i-1][j],global[i-1][j-1]) (1<=k<=i-1)

【复杂度】

时间复杂度O(kn),空间可以用滚动数组,为O(1)

【AC】

 class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int len=prices.size();
if(len== || len==) return ;
if(k==) return ;
if(k>=len/){
int ans=;
for(int i=;i<len;i++){
ans+=max(,prices[i]-prices[i-]);
}
return ans;
}
vector<int> local,global;
local.resize(len);
global.resize(len);
for(int i=;i<len;i++){
local[i]=global[i]=;
}
for(int j=;j<=k;j++){
for(int i=;i<len;i++){
local[i]=max(local[i-]+prices[i]-prices[i-],global[i-]);
}
for(int i=;i<len;i++){
global[i]=max(global[i-],local[i]);
}
}
int ans=global[len-];
return ans;
}
};

local即为dp,表示局部最优解,global表示全局最优解

注意要特判k>=n/2,leetcode没有给出数据范围,k很大时会tle或mle,k若大于n/2,相当于可以无限次买卖,贪心就可以。

【dp】leetcode Best Time to Buy and Sell Stock IV的更多相关文章

  1. 【刷题-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 ...

  2. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. [LeetCode] 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 an al ...

  4. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

  5. LeetCode——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. ...

  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 6道合集【DP】

    1. Best Time to Buy and Sell Stock 2. Best Time to Buy and Sell Stock II 3. Best Time to Buy and Sel ...

  8. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  9. Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)

    Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...

随机推荐

  1. 连接MongoDB数据库的配置说明

  2. mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

    解决方案如下:

  3. (转)SpringMVC学习(二)——SpringMVC架构及组件

    http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...

  4. The - Modcrab——使用贪心策略

    一.题目信息 The - Modcrab 简单翻译一下:Vova有生命值h1,每次攻击值为a1,每瓶药水恢复生命值c1;Modcrab有生命值h2,每次攻击值为a2.在每个关卡开始,Vova有两种选择 ...

  5. python读取.mat文件

    可以先看一下.mat中存了些什么: import scipy.io as sio box_file = '/home/bnrc/formatm/test/1479504458876408533_box ...

  6. linux——nmap端口扫描命令

    先安装 nmap :apt-get install nmap 端口扫描命令nmap -sS 172.16.55.100nmap -Pn 172.16.55.100第一组渗透测试指令,用于情报收集. 要 ...

  7. C#值类型和引用类型与Equals方法

    1. C#的值类型和引用类型 C#的对象里面有两种类型,一个是引用类型,一个是值类型,值类型和引用类型的具体分类可以看下面的分类.   在C#中,不管是引用类型还是值类型,他们都隐式继承Object类 ...

  8. 【动态规划】bzoj1575: [Usaco2009 Jan]气象牛Baric

    预处理普通动态规划:庆祝1A三连 Description 为了研究农场的气候,Betsy帮助农夫John做了N(1 <= N <= 100)次气压测量并按顺序记录了结果M_1...M_N( ...

  9. Python发行版(编译器)

    一.Python编译器简介 根据实现Python编译器语言一般分为以下几种: 1.1.CPython 标准的Python,解释型编译器. Python:标准的CPython版本,即官方发布版本. IP ...

  10. perl学习之HERE文档

    Perl的here文档机制是从UNIX shell中的here文档机制派生而来的. 和在shell中一样,Perl中的here文档也是面向行的引用表单,要求提供<<运算符,其后跟随一个初始 ...