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 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.
题目大意:最多k次交易,问可以达到的最大获利。
思路:dp, 递推式:
dp[i, j] represents the max profit up until prices[j] using at most i transactions.
dp[i, j] = max(dp[i, j-1], prices[j] - prices[jj] + dp[i-1, jj]) { jj in range of [0, j-1] }
= max(dp[i, j-1], prices[j] + max(dp[i-1, jj] - prices[jj]))
dp[i, j] 表示最多i次交易时,价格为prices[j]时的最大获利。
核心代码如下:
int[][] dp = new int[k+1][n];
for (int i = 1; i <= k; i++) {
int localMax = dp[i-1][0] - prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j-1], prices[j] + localMax);
localMax = Math.max(localMax, dp[i-1][j] - prices[j]);
}
}
188. Best Time to Buy and Sell Stock IV——LeetCode的更多相关文章
- 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 ...
- 【刷题-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 ...
- 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 买卖股票的最佳时间 IV
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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 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 ...
- 188. Best Time to Buy and Sell Stock IV
题目: 链接: 题解: 测试: Reference:
- 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...
随机推荐
- PHP Mongodb API参考
<?php /*** Mongodb类** examples: * $mongo = new HMongodb("127.0.0.1:11223"); * $mongo-&g ...
- CentOS 下 安装 nginx
1.准备 安装 nginx 之前,需要确认是否安装了 GCC,PCRE, zlib, OpenSSL 等. 如未安装,则先安装这些插件 # yum install -y gcc # yum insta ...
- jqGrid -treeGrid 按需加载
Load Rows On Demand (AJAX) 参考:http://www.guriddo.net/demo/treegridjs/
- tomcat server 报错之 More than the maximum allowed number of cookies
More than the maximum allowed number of cookies EVERE: Error processing request java.lang.IllegalArg ...
- Django组件——分页器(paginator)
一.视图层 from django.shortcuts import render # Create your views here. from .models import Book from dj ...
- bind 事件名称 命名空间
1.通过在事件名称后面添加以点号分隔的后缀来为事件名称指派命名空间 $("#button").bind("click.editMode",function(){ ...
- es6新增的数组方法和对象
es6新增的遍历数组的方法,后面都会用这个方法来遍历数组,或者对象,还有set,map let arr=[1,2,3,4,3,2,1,2]; 遍历数组最简洁直接的方法法 for (let value ...
- 关于maven包的引入net.sf.json的问题
最开始通过在pom.xml文件中加入 <dependency> <groupId>net.sf.json-lib</groupId> <artifactId& ...
- matlab练习程序(结构张量structure tensor)
根据结构张量能区分图像的平坦区域.边缘区域与角点区域. 此算法也算是计算机科学最重要的32个算法之一了.链接的文章中此算法名称为Strukturtensor算法,不过我搜索了一下,Strukturte ...
- gamemakerstudio:鼠标输入
标准鼠标输入常量: mb_left 按下鼠标左键 mb_middle 按下鼠标中键 (这可能不是在所有目标平台上都有效) mb_right 按下鼠标右键 mb_none 没有按下任何鼠标键 mb_an ...