4 Best Time to Buy and Sell Stock III_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 two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
当遇到限制次数以及求最大的要求时,很自然要联想到动规。
动规中不同的状态设计,会有不同的时间复杂度。
本题有两种解法:
(1)
我最开始想到的是下面这种解法,但是Memory Limit Exceeced.
O(n^2)的解法
很自然的我们会想到记录从开头到第i个字符中进行k次交易能够得到的最大收益,记为dp[i][k].
进行更新:dp[i][k] = max{dp[j][k-1]+maxprofit(j...i)}, 0 <= j < i
这里我们需要用到每个可能的区间中进行一次交易的最大收益,也就是I中的问题。
预先计算出所有的maxprofit的复杂度是O(n^2),dp的复杂度也是O(n^2).
Code:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
if(n == 0) return 0;
vector<vector<int>> dp(n, vector<int>(3,0));
vector<vector<int>> maxprofit(n, vector<int>(n, 0));
for(int i = 0; i < n; i++)
{
int curmin = prices[i];
int curprofit = 0;
for(int j = i+1; j < n; j++)
{
if(prices[j] < curmin) curmin = prices[j];
else{
int gap = prices[j] - curmin;
if(gap > curprofit) curprofit = gap;
}
maxprofit[i][j] = curprofit;
if(i == 0) dp[j][1] = curprofit;
}
}
for(int i = 1; i < n; i++)
{
int tmp = dp[0][1] + maxprofit[0][i];
for(int j = 1; j < i; j++)
{
if(dp[j][1] + maxprofit[j][i] > tmp) tmp = dp[j][1]+maxprofit[j][i];
}
dp[i][2] = tmp;
}
return dp[n-1][2];
}
};
(2)O(n)的解法
参考了这个:http://blog.csdn.net/linhuanmars/article/details/23236995
从上面的分析中我们很容易找出一个case, 例如有一个区间差异特别大,在很长时间内都是最优的选择,而我们的dp却需要不断的枚举一些不可能构成最终解的区间。
怎么样更聪明的定义状态呢?
上一种定义中,我们是定义(i,j)中的最大的解,这样不同的(i,j)对对应的可能是同一种解;这样我们可以用一个global变量来存储;但是由于处理的区间是不断延伸的,后面出现的数字可能和当前末尾的组合起来行程更大的区间,因此我们用一个local变量存储当前以i结尾的最大的解的值。
扩展到能够选择k个区间,我们定义:
global(i,k) 表示截止到第i天,进行k次交易能够获得的最优解(不一定以最后一天结束)
local(i,k) 表示以第i天结束的k次交易能够获得的最优解
能够得到递推式:
local[i][k] = max{global[i-1][k-1], local[i-1][k]} + prices[i] - prices[i-1];
global[i][k] = max{global[i-1][k], local[i][k]};
初始值全为零,最终解为global[n-1][2]。
Code:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
if(n == 0) return 0;
vector<vector<int>> global(n, vector<int>(3,0));
vector<vector<int>> local(n, vector<int>(3,0));
for(int j = 1; j <= 2; j++)
{
for(int i = 1; i < n; i++)
{
local[i][j] = max(global[i-1][j-1], local[i-1][j]) + prices[i] - prices[i-1];
global[i][j] = max(global[i-1][j], local[i][j]);
}
}
return global[n-1][2];
}
};
从其他博客中找到了一种O(n)的解法,思路更简单,但是不具备从2次交易推广到k次的潜力。
主要的思路就是从前往后扫描一遍,找到从0到i的一次交易的最大收益,然后从后往前扫描一遍,得到从i+1到n-1的最大收益。然后两者相加取最大即可。
传送门:http://fisherlei.blogspot.com/2013/01/leetcode-best-time-to-buy-and-sell_3958.html
4 Best Time to Buy and Sell Stock III_Leetcode的更多相关文章
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
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 买卖股票的最佳时间之四
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 III 买股票的最佳时间之三
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 II 买股票的最佳时间之二
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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- 检查Linux服务器性能
如果你的Linux服务器突然负载暴增,告警短信快发爆你的手机,如何在最短时间内找出Linux性能问题所在? 概述通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. • uptime• ...
- React的井字过三关(3)
这是React井字棋项目的最后一篇笔记,记述AI实现. 一. 是开头都会说的原理 但凡懂一点围棋的人都知道"大场"这个概念,可以浅显地把它理解为布局时棋盘上各处的要点.棋谚&quo ...
- Linux常用命令和Shell编程基础
目录相关 cd - .与.. 分别表示当前目录和父目录 - ~与$HOME 都是指当前用户的主目录 - cd – 切换到上一次所在的目录(不一定是父目录) pwd - pwd 显示当前目录 - $PW ...
- 转→js数组遍历 千万不要使用for...in...
看到一篇内容还不错,但是排版实在糟糕, 逼死强迫症患者啊,直接拉下去找原文连接,找到了,但是已经消失了···500错误... 第一次因为实在看不下去一篇博客的排版, 为了排版而转载... 转载地址:h ...
- 关于C++中的cout
最近恰好看到qq群里有讨论cout的问题,发现自己对于cout的运行机制也不怎么明白,所以就仔细研究了下这个东西. 如下是c++ reference中cout的定义 extern ostream co ...
- 【WP8.1】HttpClient网络请求、进度以及终止
工作这么长时间,起初还是喜欢用面向程序过程的思路去写代码. 慢慢的才会用面向对象的思路分析.解决问题.也算是一点点进步吧. 最近在做一个下载音乐的功能.用到了HttpClient类. 于是就简单的写了 ...
- JDBC入门学习
Introduction What's JDBC JDBC stands for Java Database Connectivity, which is a standard Java API fo ...
- 【DS】About Stack
栈 一言以蔽之,就是后进的先出(LIFO). C语言实现代码: #include<stdio.h> #include<stdlib.h> typedef struct Stac ...
- tips~function pointer
An simple example: #include<stdio.h> int plus(int a,int b) { return a+b; } int main() { int (* ...
- 【bzoj1231】[Usaco2008 Nov]mixup2 混乱的奶牛
题目描述 混乱的奶牛[Don Piele, 2007]Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S_i <= ...