【刷题-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 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的更多相关文章
- [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 ...
- 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 (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 ...
- 【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 ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 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 ...
- 【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 ...
- 【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 ...
随机推荐
- CF1141C Polycarp Restores Permutation 题解
Content 给定一个长度为 \(n-1\) 的序列 \(q\),问你是否能找到一个 \(1\sim n\) 的排列 \(p\),使得 \(\forall i\in[1,n)\),\(q_i=p_{ ...
- 金山云 KS3 Python SDK 多线程并发上传文件;下载断点续传 参考脚本
并发上传 基于py自带模块 concurrent.futures import ThreadPoolExecutor #!/usr/bin/env python3 # -*- coding:utf-8 ...
- js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到
js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到 2.图2的内容为directionkey.js的内容
- JAVA发送xml格式的接口请求
/** * * @param urlStr 接口地址 * @param xmlInfo xml格式参数数据 * @return */ public static String sendMsgXml(S ...
- centos安装宝塔命令
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh & ...
- UDP&串口调试助手用法(1)
一览 UDP 串口 常用 功能概述 概览 支持UDP通信协议: 广播.单播.组播 支持串口通信 配置了常用的配置,常用的进制转化: 2进制,8进制,10进制,和16进制之间的转换 配置了 计算器,加减 ...
- 【LeetCode】872. Leaf-Similar Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 先序遍历 后序遍历 日期 题目地址:htt ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 1479 小Y的数论题
小Y喜欢研究数论,并且喜欢提一些奇怪的问题.这天他找了三个两两互质的数a, b, c,以及另一个数m, 现在他希望找到三个(0, m)范围内的整数x, y, z,使得 (xa+yb) Mod m=(z ...
- CSS实现水平垂直居中的方式有哪些?
CSS实现水平垂直居中的方式有哪些? 基本结构样式: .box { width: 400px; height: 400px; background-color: red; } .inner { wid ...