1. Best Time to Buy and Sell Stock IV

Say you have an array for which the i-th 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).

Example 1:

Input: [2,4,1], k = 2
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

Input: [3,2,6,5,0,3], k = 2
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.

动态规划

用一个二维矩阵trans,矩阵的列表示交易的价格,矩阵的行表示交易的次数,矩阵的值表示当前能够获得的最大利润

以允许3交易为例,考察第3天的最大利润trans[3][3]

  • 第3天什么都不做,\(trans[3][3] = trans[3][2]\)
  • 以第1天价格买入,第3天价格卖出,\(trans[3][3]=prices[3]-prices[1] + trans[2][1]\)
  • 以第2天价格买入,第3天价格卖出,\(trans[3][3]=prices[3]-prices[2] + trans[2][2]\)

取最大值即为结果。实现如下:

用两个数组buy和sell来分别表示第i次买入交易的最大收益值和第i次卖出交易的最大收益值

1.第i次买操作买下当前股票之后剩下的最大利润为第(i-1)次卖掉股票之后的利润-当前的股票价格.状态转移方程为:

    buy[i] = max(sell[i-1]- curPrice, buy[i]);

2.第i次卖操作卖掉当前股票之后剩下的最大利润为第i次买操作之后剩下的利润+当前股票价格.状态转移方程为:

    sell[i] = max(buy[i]+curPrice, sell[i]);

class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if(prices.size() ==0) return 0;
int len = prices.size(), ans =0;
if(k >= len/2){
for(int i = 1; i < len; i++)
if(prices[i]-prices[i-1]>0)ans += prices[i]-prices[i-1];
return ans;
}
vector<int> buy(len+1, INT_MIN), sell(len+1, 0);
for(auto val: prices)
{
for(int i =1; i <= k; i++)
{
buy[i] = max(sell[i-1]-val, buy[i]);
sell[i] = max(buy[i]+val, sell[i]);
}
}
return sell[k];
}
};

Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock III

[Best Time to Buy and Sell Stock IV](

【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV的更多相关文章

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

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

  4. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

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

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

  6. [LeetCode][Java] Best Time to Buy and Sell Stock IV

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

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

  8. 【leetcode刷题笔记】Best Time to Buy and Sell Stock II

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

  9. 【leetcode刷题笔记】Best Time to Buy and Sell Stock III

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

随机推荐

  1. CF1473B String LCM 题解

    Content 如果一个字符串 \(s\) 由若干个字符串 \(t\) 拼接而成,则我们说 \(s\) 能被 \(t\) 整除.定义 \(s_1,s_2\) 的最短公倍串为可以同时被 \(s_1,s_ ...

  2. java 编程基础:【注解】 提取注解信息,利用自定义注解编写测试类,注解绑定事件

    提取注解信息 使用注解修饰了类.方法.成员变量等成员之后,这些注解不会自己生效,必须由开发者提供相应工具来提取并处理注解信息.   Java使用java.lang.annotation.Annotat ...

  3. 当是class com.cosl.po.Pc$$EnhancerByCGLIB$$38c58f03时,反射属性都他妈不好用了

    当是class com.cosl.po.Pc$$EnhancerByCGLIB$$38c58f03时,反射属性都他妈不好用了 搞不懂为什么?

  4. flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8

    flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8

  5. IO复用的三种方法(select,poll,epoll)深入理解

    (一)IO复用是Linux中的IO模型之一,IO复用就是进程告诉内核需要监视的IO条件,使得内核一旦发现进程指定的一个或多个IO条件就绪,就通过进程处理,从而不会在单个IO上阻塞了,Linux中,提供 ...

  6. 网络编程之UDP中一个包的大小最大能多大

    读书笔记:here 结论1:局域网环境下,建议将UDP数据控制在1472字节以下 一定要知道 因为链路层的传输单元(MTU)是1500字节,1500字节中并不包含链路层的首尾18个字节.1500字节是 ...

  7. c++代码编译错误查找方法之宏

    1.关于 本文演示环境: win10+vs2017 好久不用这法子了,都快忘了 排查错误,思路很重要,且一定要思路清晰(由于自己思路不清晰,查找错误耽误了不少时间,其实问题很简单,只是你要找到他需要不 ...

  8. idea使用教程-常用设置

    [1]进入设置: [2]设置主题: [3]编辑区的字体变大或者变小: [4]鼠标悬浮在代码上有提示: [5]自动导包和优化多余的包: 手动导包:快捷键:alt+enter 自动导包和优化多余的包: [ ...

  9. 4 种主流的 API 架构风格对比

    1RPC:调用另一个系统的函数 RPC 的工作机制 客户端调用一个远程的过程,将参数和附加信息序列化为消息,然后将消息发送到服务端.服务端在接受到消息后,将信息的内容反序列化,执行所请求的操作,然后将 ...

  10. 第五个知识点 复杂性为NP类是什么意思

    第五个知识点 复杂性为NP类是什么意思 原文地址:http://bristolcrypto.blogspot.com/2014/11/52-things-number-5-what-is-meant- ...