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

分析

依然是stock problem模型,但这次必须压缩空间,否则超限,并且在 k>prices.size() 时,转化为k=+无穷的情况,否则超限。

const int inf=-999999;
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if(prices.size()<=1) return 0;
if (k >= prices.size()) {
int T_ik0 = 0, T_ik1 =inf;
for (auto price : prices) {
T_ik1 = max(T_ik1, T_ik0 - price);
T_ik0 = max(T_ik0, T_ik1 + price); }
return T_ik0;
}
int sell[k+1]={0},buy[k+1];
fill(buy,buy+k+1,inf);
for(int i=0;i<prices.size();i++)
for(int j=1;j<=k;j++){
sell[j]=max(sell[j],buy[j]+prices[i]);
buy[j]=max(buy[j],sell[j-1]-prices[i]);
}
return sell[k];
}
};

LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)的更多相关文章

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

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

  3. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

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

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

  8. 【刷题-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 ...

  9. 【leetcode】Best Time to Buy and Sell 3 (hard) 自己做出来了 但别人的更好

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. javascript 冒泡与捕获的原理及操作实例

    所谓的javascript冒泡与捕获不是数据结构中的冒泡算法,而是javascript针对dom事件处理的先后顺序,所谓的先后顺序是指针对父标签与其嵌套子标签,如果父标签与嵌套子标签均有相同的事件时, ...

  2. Ghost系统操作记录

    1.下载Symantec Ghost应用. 2.下载老毛桃PE工具箱. 3.利用老毛桃PE工具箱制作启动U盘. 4.拷贝Ghost应用至U盘. 5.设置计算机启动顺序为U盘启动. 6.重启计算机进入P ...

  3. visual studio 2015 key vs2015密钥

    Visual Studio Professional 2015简体中文版(专业版)KEY:HMGNV-WCYXV-X7G9W-YCX63-B98R2Visual Studio Enterprise 2 ...

  4. fullpagejs实现的拥有header和foooter的全屏滚动demo/fullpage footer

    fullpagejs实现的拥有header和foooter的全屏滚动, 技术要点:给section元素加fp-auto-height类, <!DOCTYPE html> <html ...

  5. javascript之input获取的时间减1秒&&t时间恢复

    将输入得到的时间减少1秒:20:00:00  ———  19:59:59    方法一:普通时间转换 endDateMap(date){ var h = new Date(date).getHours ...

  6. php(一)

    PHP (Hypertext preprocessor 超文本预处理器) 1.环境工具 Xampp等工具 2.apache配置 默认的Apache路径是  c:/xampp/apache 文件夹 可以 ...

  7. 机器学习-Probabilistic interpretation

    Probabilistic interpretation,概率解释  解释为何线性回归的损失函数会选择最小二乘 表示误差,表示unmodeled因素或随机噪声,真实的y和预测出来的值之间是会有误差的, ...

  8. 短视频SDK在广电系统的解决方案

    锐动视频编辑SDK集手机视频拍摄和视频剪辑主要功能于一体,同时包含手机端视频配音配乐,字幕特效,滤镜,转场特效等各种功能,全方位满足开发者的需求,并可以快速植入到APP中. 手机端的摄像头录制和直播功 ...

  9. ubuntu命令行使用ftp客户端

    转载 本篇文章主要介绍在Ubuntu 8.10下如何使用功能强大的FTP客户端软件NcFTP. Ubuntu的源里为我们提供了FTP客户端软件NcFTP,可这款工具对新手来说不是很方便.本文介绍的是一 ...

  10. linux 10201 ASM RAC 安装+升级到10205

    准备环境的时 ,要4个对外IP,2个对内IP 不超过2T,,一般都用OCFS 高端存储适合用ASM linux10G安装的时候,安装的机器时间要小于等于(如果是等于要严格等于)第二个机器的时间(只有l ...