【dp】leetcode Best Time to Buy and Sell Stock IV
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的更多相关文章
- 【刷题-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 ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 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 ...
- 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. ...
- 【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 ...
- [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 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 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. ...
随机推荐
- SQL 数学串函数
数学函数 ceiling 取上限 floor 取下限 round 四舍五入 len 长度 abs 绝对值 PI()圆周率 sqrt 开根号 qwuare 平方根 select 10 ...
- Codeforces Gym 100650B Countdown (离线)
题目链接:http://codeforces.com/gym/100650 根据给出的树和d,求出一些结点,这些结点形成子树的第d层结点数应该尽量多,具体要求可以参考题目. dfs一个结点前保存询问深 ...
- 手把手教你打造一个 Mac 风格的 Windows10(手动滑稽)
Mark https://www.sqlsec.com/2018/04/winmac.html 大佬写得很好,资瓷!! MyDock可能不是最新的,给出官方维护的网盘:https://pan.bai ...
- CentOS7——防火墙设置
1.查看firewall服务状态 systemctl status firewalld 2.查看firewall的状态firewall-cmd --state 3.开启.重启.关闭.firewalld ...
- JavaScript异步仿同步(控制流)的实现
在前端开发中尤其是在nodejs开发中经常会遇到这样的场景(以ajax为例):有3个(或者更多个)Ajax请求,并且第2个请求依赖于第1个,第3个请求依赖于第2个,那我们可能就会在发第一个Ajax后回 ...
- 【转】数据库SQL的一些总结
http://www.cnblogs.com/yank/category/104903.html
- fossil 代理设置
C:\>fossil user new Joe C:\>fossil user default Joe 设置账户 fossil setting proxy http://-:-fossil ...
- SQLyog连接数据库 提示错误plugin caching_sha2_password could not be loaded
1.打开mysql cmd 2.执行语句 ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; # ...
- 安装ruby开发环境
如何快速正确的安装 Ruby, Rails 运行环境 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 ...
- nodejs 设置安装包路径的取消和安装cnpm
安装cnpm: $ npm install -g cnpm --registry=https://registry.npm.taobao.org 配置nodejs的npm安装包路径: npm conf ...