188. Best Time to Buy and Sell Stock IV (Array; DP)
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).
思路:用状态存储至当前日为止第jth buy/sell的最大利润。到了第二天,我们可以按j从大到小(因为j大的新状态依赖于之前j小的状态),修改这个状态。
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int dates = prices.size();
if(dates <= || k == ) return ;
if (k >= prices.size()) return maxProfit2(prices); //unlimited transaction
vector<int> release(k,); //sell stock
vector<int> hold(k,INT_MIN); //buy stock
for(int i = ; i < dates; i++){
for(int j = k-; j > ; j--){
release[j] = max(release[j], hold[j]+prices[i]); //jth sell happen at ith day
hold[j]=max(hold[j], release[j-]-prices[i]); //jth buy happen at ith day
}
release[] = max(release[], hold[]+prices[i]);
hold[] = max(hold[],-prices[i]);
}
return release[k-];
}
int maxProfit2(vector<int> &prices) {
int profit = ;
for (int i=; i<(int)prices.size()-; i++) {
if (prices[i+] > prices[i])
profit += prices[i+] - prices[i];
}
return profit;
}
};
188. Best Time to Buy and Sell Stock IV (Array; DP)的更多相关文章
- 【刷题-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 ...
- 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 ...
- [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 ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 123. Best Time to Buy and Sell Stock III (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 188. Best Time to Buy and Sell Stock IV leetcode解题笔记
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 188. Best Time to Buy and Sell Stock IV——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 188. Best Time to Buy and Sell Stock IV
题目: 链接: 题解: 测试: Reference:
随机推荐
- mybatis_动态sql 查询、更新
1)sql where 条件 select id="find" parameterType="User" resultType="User" ...
- Leetcode 题解 Jump Game
一,笨方法 o(n^2).果然看完别人的解法就自惭形秽啊!! 我用的动态规划方法. 比如输入 i: 0 1 2 3 4 ———————————————— a[i]: 2 3 1 0 4 直接利用原来 ...
- supervisor process management
supervisor是unix like系统的进程管理工具 安装: pip install supervisor 配置文件: echo_supervisord_conf # 打印一个配置文件样例 ec ...
- Mysql(MyISAM和InnoDB)及Btree和索引优化
MYSQL 一.引擎 mysql:MySQL是一个关系型数据库管理系统,其中有两种引擎最为常见MyISAM和InnoDB MyISAM(非聚集索引) MySQL 5.0 之前的默认数据库引擎,最为常 ...
- mysql主从复制搭建中几种log和pos详解
一.主从原理 Replication 线程 Mysql的 Replication 是一个异步的复制过程,从一个 Mysql instace(我们称之为 Master)复制到另一个 Mysql in ...
- 遍历DOM树,理解更新范围
在JavaScript中,如果需求对多个元素重复进行同样的操作,就需要写一个循环来遍历选中的所有元素. 在jQuery中,当选择器返回了多个元素时,可以使用一个方法来更新所有的元素,不再需要使用循环. ...
- 吴裕雄 13-MySQL UPDATE 查询
以下是 UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法:UPDATE table_name SET field1=new-value1, field2=new-value2[WHE ...
- Linux查看进程运行的完整路径方法
通过ps及top命令查看进程信息时,只能查到相对路径,查不到的进程的详细信息,如绝对路径等.这时,我们需要通过以下的方法来查看进程的详细信息: Linux在启动一个进程时,系统会在/proc下创建一个 ...
- Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)
这是因为测试代码时遇到错误,它会停止编译.只需要在pom.xml的<project>里添加以下配置,使得测试出错不影响项目的编译.<build> <plugins> ...
- iview 表单验证
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...