Best Time to Buy and Sell Stock IV 解答
Question
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.
For example, given prices = [4,4,6,1,1,4,2,5], and k = 2, return 6.
Answer
用DP解答。
local[i][j] 表示0~i的数,最多j次交易,并且最后一次交易必须包含prices[j](即最后一天必须卖出),收益最大值。
global[i][j] 表示0~i的数,最多j次交易,收益最大值。
diff = prices[i] - prices[i-1] local[i][j] = max(global[i-1][j-1] + max(diff,0),local[i-1][j]+diff) global[i][j] = max(local[i][j], global[i-1][j])
local[i-1][j] + diff 表示第i-1天,买进prices[i-1],第i天卖出。
由于local[i-1][j]表示最后一次交易必须包含prices[i-1],即prices[i-1]必须卖出。所以可以把第i-1天的交易和第i天的交易合并,即成了最多j次交易,最后一天交易必须卖出prices[i]。 global[i-1][j-1] 表示前i-1天,最多j-1次交易。最后一天比较diff,如果diff>0,则第i-1天买进,第i天卖出;如果diff<0,则第i天买进,第i天卖出。
class Solution {
/**
* @param k: An integer
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int k, int[] prices) {
// write your code here
if (k == 0) {
return 0;
}
if (k >= prices.length / 2) {
int profit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
int n = prices.length;
int[][] mustsell = new int[n + 1][n + 1]; // mustSell[i][j] 表示前i天,至多进行j次交易,第i天必须sell的最大获益
int[][] globalbest = new int[n + 1][n + 1]; // globalbest[i][j] 表示前i天,至多进行j次交易,第i天可以不sell的最大获益
mustsell[0][0] = globalbest[0][0] = 0;
for (int i = 1; i <= k; i++) {
mustsell[0][i] = globalbest[0][i] = 0;
}
for (int i = 1; i < n; i++) {
int gainorlose = prices[i] - prices[i - 1];
mustsell[i][0] = 0;
for (int j = 1; j <= k; j++) {
mustsell[i][j] = Math.max(globalbest[(i - 1)][j - 1] + gainorlose,
mustsell[(i - 1)][j] + gainorlose);
globalbest[i][j] = Math.max(globalbest[(i - 1)][j], mustsell[i ][j]);
}
}
return globalbest[(n - 1)][k];
}
};
Best Time to Buy and Sell Stock IV 解答的更多相关文章
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 【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之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...
- 【刷题-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 ...
- [LeetCode] 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 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 Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- Lintcode393 Best Time to Buy and Sell Stock IV solution 题解
[题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...
- [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 ...
随机推荐
- Qt Style Sheets Reference
Qt Style Sheets support various properties, pseudo-states, and subcontrols that make it possible to ...
- git pull 代码很慢的问题
办公环境调整,之前开发机是和自己的电脑放同一网段内的,现在开发机放至到本地其他网段内,造成pull 代码很慢的问题,在网上查了一下 以下是原文,链接为 http://blog.sina.com.cn/ ...
- GetJsonByDataTable
public string getJsonByModel(DataTable dt) { StringBuilder nsb = new StringBuilder(); ; i < dt.Ro ...
- java虚拟机涉及内存溢出
Java语言写的代码是.java文件,它会被特定程序编译(javac.exe,它会被Eclipse之类的IDE调用)成字节码(bytecode),字节码不能直接在CPU上运行,需要另一个程序读取并执行 ...
- 托盘图标、气泡以及任务栏崩溃后的自动添加——Shell_NotifyIcon
托盘图标使用函数 Shell_NotifyIcon 创建.修改和删除,参数主要使用 NOTIFYICONDATA 结构. 任务栏启动时会给所有顶层窗口发送 TaskbarCreated 消息,由于不同 ...
- Ignatius and the Princess I
算法:搜索+优先队列+(递归输出结果) The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 编辑一个类库项目 即*.csproj这个文件的正确方式
以前总是用记事本打开,删除一些或增加一些已修改的文件 今天才知道,正确的方式为: 右键单击类库,选择“卸载项目”,然后再右键单击已卸载变为灰色的类库,选择“编辑*.csproj” 编辑完了重新加载一下 ...
- CentOS下几种软件安装方式
1.rpmRPM RedHat Package Manager(RedHat软件包管理工具)的缩写,这一文件格式名称虽然打上了RedHat的标志, 但是其原始设计理念是开放式的,现在包括OpenLin ...
- Spring4.0学习笔记(3) —— Spring_Bean之间的关系
1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Box model小心得
最近在研究css~当设置一个元素width后~有时候也会对他设定padding,margin,border值, 每次这样我就心里琢磨,那这个元素的width会变吗,js获取元素的宽度width()指的 ...