您的位置:首页 > 其它

**[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode

2017-01-17 11:02 465 查看
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.

Example

Given prices = 
[4,4,6,1,1,4,2,5]
,
and k = 
2
, return 
6
.

分析:使用二维数组,res[i][j]代表0~i中,交易j次的最大收益。

状态转移方程为:

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][j]时,i-1,交易了j次,并且位置i因为和第j次交易相邻,可以并入第j次交易的情况,所以需要使用两个数组,一个数组local记录以i为最后一次卖出时,最大收益,和一个数组global,记录0到i范围内整体最大收益。这样local可以用来计算之前提到的交易合并问题。

以下方法运行内存超出规定大小。

class Solution {
/**
* @param k: An integer
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length == 0) return 0;
int[][] local = new int[prices.length][k + 1];
int[][] global = new int[prices.length][k + 1];

int res = 0;
for(int j = 1; j <= k; j++) {
for(int i = 1; i < prices.length; i++) {
int diff = prices[i] - prices[i - 1];

local[i][j] = Math.max(local[i - 1][j] + diff,
global[i - 1][j - 1] + Math.max(0, diff));

global[i][j] = Math.max(global[i - 1][j], local[i][j]);
}
}
return global[prices.length - 1][k];
}
};
考虑使用两个滚动数组代替。滚动数组要确保覆盖后的值不会被用到。因此又引入了临时变量tmp。

现在内存符合要求,但是超时。继续改。

class Solution {
/**
* @param k: An integer
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length == 0) return 0;
int[] global = new int[prices.length];
int[] local = new int[prices.length];

int res = 0;
for(int j = 1; j <= k; j++) {

int tmp = global[0];
for(int i = 1; i < prices.length; i++) {
int diff = prices[i] - prices[i - 1];
local[i] = Math.max(local[i - 1] + diff,
tmp + Math.max(0, diff));

tmp = global[i];
global[i] = Math.max(global[i - 1], local[i]);
if(global[i] > res) res = global[i];
}
}
return res;
}
};

当i小于j时,例如3个交易日需要进行4次交易的情况,可以以排除,所以给i加上限制。i>=j.  通过测试。

class Solution {
/**
* @param k: An integer
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length == 0) return 0;
int[] global = new int[prices.length];
int[] local = new int[prices.length];
//Memory Limit Exceeded,改用两个一维滚动数组

int res = 0;
for(int j = 1; j <= k; j++) {

int tmp = global[0];
for(int i = j; i < prices.length; i++) {
int diff = prices[i] - prices[i - 1];
local[i] = Math.max(local[i - 1] + diff,
tmp + Math.max(0, diff));

tmp = global[i];
global[i] = Math.max(global[i - 1], local[i]);
if(global[i] > res) res = global[i];
}
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐