188. Best Time to Buy and Sell Stock IV (Array; DP)

时间:2022-08-30 14:56:29

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. 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. &lbrack;LeetCode&rsqb; 188&period; 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】188&period; Best Time to Buy and Sell Stock IV 解题报告(Python)

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

  4. LeetCode 188&period; Best Time to Buy and Sell Stock IV &lpar;stock problem&rpar;

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

  5. 123&period; Best Time to Buy and Sell Stock III &lpar;Array&semi; DP&rpar;

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

  6. 188&period; 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 ...

  7. 188&period; 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&period; Best Time to Buy and Sell Stock IV

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

  9. 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV

    假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...

随机推荐

  1. jQuery获取带点的id元素

    一般jQuery获取某个id为elem元素,只需用$('#elem')就行了,但是如果id中不小心包括了'.' ,那么我吗就会发现这时候jQuery就不能获取到这个元素了.但是使用dom原生的getE ...

  2. NOJ1019-计算二叉树的高度和结点数

    输入 二叉树的先序遍历序列,用#代表空树或空子树. 输出 共五行 前三行依次输出先序.中序和后序遍历序列, 第四行输出二叉树的高度, 第五行依次输出二叉树总结点数目.叶子结点数目.度为1的结点数目. ...

  3. Red hat Linux 安装Node&period;js 源码安装

    1. 下载源码包 http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz 2.准备安装环境,>python2.6, gcc, g++ pytho ...

  4. poj2月题解

    竟然生日前一天poj破百,不错不错,加速前进! poj2437 由于泥泞不重叠,所以按其实左边排个序再统计一遍即可(如果不是刚好盖满就尽量往后盖) poj2435 细节bfs poj2230 求欧拉回 ...

  5. http&colon;&sol;&sol;blog&period;csdn&period;net&sol;zhang&lowbar;xinxiu&sol;article&sol;details&sol;38655311

    一.Activiti下载及简介 1.1.Activiti下载 官网下载地址:http://activiti.org/download.html        Note:下载时不一定要使用最新版本的,最 ...

  6. MFC 控件用法

    1:IP Control 变量CIPAddressCtrl m_iAddr 关联DDX_Control(pDX,IDC_IPADDRESS1,m_iAddr); 设置地址:m_iAddr.SetAdd ...

  7. 小白也能看懂插件化DroidPlugin原理(一)-- 动态代理

    前言:插件化在Android开发中的优点不言而喻,也有很多文章介绍插件化的优势,所以在此不再赘述.前一阵子在项目中用到 DroidPlugin 插件框架 ,近期准备投入生产环境时出现了一些小问题,所以 ...

  8. &lbrack;Swift&rsqb;LeetCode172&period; 阶乘后的零 &vert; Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  9. 【CF1119D】Frets On Fire

    题目大意:给定一个长度为 n 的序列,给定一个恒定的 w,求解 \[\sum\limits_{i=1}^{n}min\{d[i],w\}\] 题解:学会了对最小值和式的快速处理. 若在下标的角度考虑, ...

  10. 用java数组模拟购物商城功能与实现

    实体类1(商品): package mall.model; public class goods { private int shoppingID; // 商品编号 private String sh ...