动态规划——Best Time to Buy and Sell Stock III
2024-10-13 22:52:36
题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果最多进行两次交易,但必须在买进一只股票前清空手中的股票,求最大的收益。
示例 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.
示例 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.
示例 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
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.
示例 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.
示例 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
之前有一个Best Time to Buy and Sell Stock I ,比这个题简单点儿,那个题是最多一次操作,求最大的收益。同样是使用动态规划,由于这次允许最多两次操作,只需要分两次,
将输入的整个周期通过for对周期长度限制(第一个子周期长度递增且不超过原周期长)并分割成两个子周期先后进行dp数组的递推即可。
int maxProfit(int* prices, int pricesSize) {
int ans = ;
if (pricesSize > ){
int*dp = (int*)malloc(pricesSize*sizeof(int));
dp[] = ;
int ans2 = ;
for (int i = ; i <= pricesSize; i++){
dp[] = ;
ans2 = ;
for (int j = ; j<i; j++){
dp[j] = (prices[j] + dp[j - ] - prices[j - ]) > ? (prices[j] + dp[j - ] - prices[j - ]) : ;
ans2 = ans2 >= dp[j] ? ans2 : dp[j];
}
ans = ans >= ans2 ? ans : ans2;
if (i < pricesSize)dp[i] = ;
for (int k = i+; k<pricesSize; k++){
dp[k] = (prices[k] + dp[k - ] - prices[k - ]) > ? (prices[k] + dp[k - ] - prices[k - ]) : ;
ans = ans >= (dp[k]+ans2) ? ans : (dp[k]+ans2);
}
}
//free(dp);
}
else if (pricesSize == ){
ans = (prices[] - prices[]) > ? (prices[] - prices[]):;
}
else ans = ;
return ans;
}
但是非常搞笑的是我这个代码虽然在LeetCode上成功AC,但是应该是最差的一个解决方案。。。但是思路上非常便于理解
最新文章
- gulp教程之gulp-livereload
- 基于MATLAB的adaboost级联形式的人脸检测实现
- oracle11g密码大小写敏感问题
- D3画图学习一
- bootstrap jQuery Ztree异步载入数据,check选择&;amp;可加入、改动、删除节点
- Java虚拟机参数设置(转)
- 【&#9733;】微信之于QQ的市场哲学
- 第二次项目冲刺(Beta阶段)--第七天
- include、include_once、require、require_once其区别
- c++趣味之为变参模板的每个参数执行单独函数
- DUMP3.5 企业级电商项目
- 梳理:python—同一个类中的方法调用
- Python之元组方法
- where are you?
- minicom for Mac 配置
- Qt的action控件中采用默认绑定,没有connect显示绑定!!!
- 用java构造一个带层次的文件目录遍历器
- matplotlib画图无法显示图例 报错No handles with labels found to put in legend.
- JDK(一)JDK集合框架
- visibility:hidden和display:none的区别
热门文章
- maven 使用 log4j
- 题解-洛谷P1981 表达式求值(模拟+处理优先级的递归)
- JS学习笔记Day16
- WebSocke实时通讯协议
- 通过Visualizing Representations来理解Deep Learning、Neural network、以及输入样本自身的高维空间结构
- 【转载】VS中生成、清理项目、调试、开始执行(不调试)、Debug 和 Release等之间的区别
- [再寄小读者之数学篇](2014-06-23 积分不等式 [中国科学技术大学2013年高等数学B 考研试题])
- webpack安装异常
- iTOP-4412开发板-串口转接小板的使用文档
- python正则表达式--编译正则表达式re.compile