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)的更多相关文章

  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. 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 ...

  3. [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 ...

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

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

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

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 188. Best Time to Buy and Sell Stock IV

    题目: 链接: 题解: 测试: Reference:

随机推荐

  1. 【ASP.NET 进阶】PDF文件在线预览(类似百度文库)

    工作需要完成文档的在线预览,现在完成了第一步PDF文件的预览,步骤是通过PDF转换工具pdf2swf.exe把PDF文件转换为SWF文件,然后通过FlexPaper就可以预览了.效果如下(GIF图片太 ...

  2. TypeError: 'range' object doesn't support item deletion

    python 是个逐步迭代开发的过程,他不是向下兼容的,更不是向上兼容,版本不一致,好端端的程序就是不能运行了. 下面是在python 2中能运行,在Python 3中不能运行的代码.其实也很简单.但 ...

  3. sql 2014 安装失败

    SQL Server setup failed to modify security permissions on 原因是 上述目录中没有权限,浏览此文件夹试试,有 错误,删除文件夹,无权删除,通过右 ...

  4. 机器学习进阶-图像基本操作-边界补全操作 1.cv2.copyMakeBoder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE) 进行边界的补零操作 2.cv2.BORDER_REPLICATE(边界补零复制操作)...

    1.cv2.copyMakeBoder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE) 参数说明: i ...

  5. 现学现卖——VS2013 C#测试

    VS2013 C#测试 首先安装Unit Test Generator.方法为:工具->扩展和更新->联机->搜索“Unit Test Generator”,图标为装有蓝色液体的小试 ...

  6. 多媒体基础知识之PCM数据《 转》

    多媒体基础知识之PCM数据 1.什么是PCM音频数据 PCM(Pulse Code Modulation)也被称为脉冲编码调制.PCM音频数据是未经压缩的音频采样数据裸流,它是由模拟信号经过采样.量化 ...

  7. ORM查询api

    下面的方法都是对查询的结果进行出理:比如objects.filter.values()... 1)values(*field):返回一个可迭代的字典序列<QuerySet: [{name='小王 ...

  8. 配置nginx实现windows/iis应用负载均衡(转载)

    配置nginx实现windows/iis应用负载均衡   nginx是俄罗斯人开发的一款跨平台的高性能HTTP和反向代理服务器,可以利用它实现web应用服务器的负载均衡. 反向代理是指将用户请求通过代 ...

  9. 如何使用JDBC查询指定的记录

    //连接数据库 public class JdbcDao {    private Connection conn=null;    private String strSql=null; publi ...

  10. JS实现简单的运行代码 & 侧边广告

    /* JS实现简单的运行代码功能 */<!doctype html> <html> <head> <meta charset="utf-8" ...