【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. ...
随机推荐
- jmeter动态参数传值配置
jmeter动态参数传值配置
- Sqlserver 2012 Always on技术
使用了Sqlserver 2012 Always on技术后,假如采用的配置是默认配置,会出现Primary server CPU很高的情况发生,比如默认配置如下: 需要自定义来解决这个问题. 我们先 ...
- Grace Huang 2017/1/12
原文 Huang doesn't think of acting as pretending to be someone else.Rather,she considers it an opportu ...
- ImportError: No module named flask.ext.wtf 解决方法
install pip install flask.ext.wtf
- (转)MyBatis框架的学习(五)——一对一关联映射和一对多关联映射
http://blog.csdn.net/yerenyuan_pku/article/details/71894172 在实际开发中我们不可能只是对单表进行操作,必然要操作多表,本文就来讲解多表操作中 ...
- 51nod 1276 1276 岛屿的数量 (很好玩的题目
题意: 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没.原本的大岛屿则会分为多个小岛屿,如果海平面一直上升,则所有岛都会被淹没在水下. 给出N个岛的高度.然后有 ...
- SQL与脚本语言
SQL是人类与数据库沟通的语言https://zhidao.baidu.com/question/413397944.html我个人认为SQL是一种专门对数据库进行操作的特殊的脚本语言.因为SQL语句 ...
- js 返回上一页并刷新页面
js 方法 代码如下 self.location=document.referrer;
- HTML5 Canvas奇幻色彩Loading加载动画
转自 https://www.html5tricks.com/tag/loading%E5%8A%A8%E7%94%BB/
- [LUOGU] P1387 最大正方形
题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...