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. kubernetes安装过程报错及解决方法

    1.your configuration file uses an old API spec: "kubeadm.k8s.io/v1alpha2". 执行kubeadm init ...

  2. leetcode41

    package main import ( "fmt" ) func firstMissingPositive(nums []int) int { m := make(map[in ...

  3. Mysql IN语句查询

    语法: WHERE column IN (value1,value2,...) WHERE column NOT IN (value1,value2,...) 1.in 后面是记录集,如: selec ...

  4. ubuntu16.04搭建geodjango+postgresql+postgis的WebGIS框架(三)加载空间数据

    之前两篇基本上搭好了geodjango开发的环境了,当然你的电脑上肯定要有python和django的环境(这个我就不介绍了,网上一搜一大堆),我自己用的python3.5和django2.0(毕竟2 ...

  5. gulp 添加版本号 解决浏览器缓存问题

    分别安装gulp-rev.gulp-rev-collerctor.gulp-asset-rev,安装了的略过 npm install gulp-rev --save-dev npm install g ...

  6. maven使用fingbugs插件

    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plu ...

  7. APP-2.1-Hbuilder与夜神 & HbuilderX与夜神模拟器连接

    经上一步完成Hbuilder.HbuilderX与夜神模拟器的安装,本次介绍下两者之间的连接设置. 1.三者的安装路径 Hbuilder:E:\SAP UI5\HBuilder HbuilderX:D ...

  8. 关于vector变量的size,是一个无符号数引发的bug。LeetCode 3 sum

    class Solution { public: vector<vector<int>> threeSum(vector<int>& a) { vector ...

  9. java解决查找问题

    1.给定一个字符串,找到里面的大写字母和小写字母以及其他字母的个数: 代码: package test; public class Stringclass { public static void m ...

  10. vb的VSFlexGrid控件

    多行选中 VSFlexGrid的SelectionMode = flexSelectionListBox,现在可以配合Ctrl进行多行选择 循环取值 用vsflexgrid.SelectedRows  ...