**[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode
来源:互联网 发布:高考背单词软件 编辑:程序博客网 时间:2024/04/27 22:58
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
.
状态转移方程为:
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; }};
0 0
- **[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- 【LintCode】 Best Time to Buy and Sell Stock 买卖股票的最佳时机
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
- 【LintCode】 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- [Java语言] [Leetcode] Best Time to Buy and Sell Stock 买卖股票的最佳时机
- Leetcode 188 Best Time to Buy and Sell Stock IV 至多k次买卖股票最大收益
- Best Time to Buy and Sell Stock 买卖股票的最佳时机
- Best Time to Buy and Sell Stock II 多次买卖股票的最佳时机
- Best Time to Buy and Sell Stock III 两次买卖股票的最佳时机 (重重重)
- 149.Best Time to Buy and Sell Stock-买卖股票的最佳时机(中等题)
- Leetcode121 Best Time to Buy and Sell Stock(买卖股票的最佳时机1)
- [Lintcode] Best Time to Buy and Sell Stock IV
- [LeetCode] Best Time to Buy and Sell Stock IV
- leetcode: Best Time to Buy and Sell Stock IV
- LeetCode(188) Best Time to Buy and Sell Stock IV
- 设计模式总结
- 排序算法之快速排序java实现
- shell 判断postgresql中用户是否存在?
- iOS学习笔记-004.加法计算器
- Secure CRT 配色方案
- **[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode
- codefoeces 755B
- 万能地图下载器下载谷歌卫星地图在CAD中套合
- 《爱上Processing》教学笔记(三)
- 关于MySQL的在线扩容
- 分割线
- mysql 定时器案例
- mysql创建函数(在phpmyadmin成功运行)
- 如何关闭登录界面并跳到另一个界面