123. Best Time to Buy and Sell Stock III (Array; DP)
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).
法I:把要求的东西profits设成状态。
第一次:从左往右扫描,同I一样记录下minimum value,不同的是,不仅要知道最大profit,还要记录下每个当前位置的profit(需要一个数组),为了之后与第二次扫描的结果相加。
第二次:从右往左scan,记录下maximum value, 计算profit,再加上之前第一次扫描从0到当前位置的profit,得到总的profit。
时间复杂度O(n) *2
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int size = prices.size();
int maxProfit = ;
int* profits = new int [size]; //表示从0到i的最大利润
int* profits_from_last = new int [size]; //表示从i到size-1的最大利润
//initial status
profits[] = ;
int minValue = prices[];
//scan from start
for(int i = ; i< size; i++)
{
if(prices[i] < minValue) //save the minimum value
{
minValue = prices[i];
profits[i] = profits[i-];
}
else //caculate the profit from minimum value to currentand compare to previous maximum profit(profits[i-1])
{
profits[i] = max(prices[i]-minValue,profits[i-]);
}
}
//initial status
profits_from_last[size-] = ;
int maxValue = prices[size-];
//scan from last
for(int i = size-; i >= ; i--)
{
if(prices[i] > maxValue) //save the maximum value
{
maxValue = prices[i];
profits_from_last[i] = profits_from_last[i+];
}
else //caculate the profit from current to maximum value and compare to previous maximum profit(profits_from_last[i+1])
{
profits_from_last[i] = max(maxValue-prices[i],profits_from_last[i+]);
}
int profit = profits[i] + profits_from_last[i]; //calculate profits of two transactions
if(profit>maxProfit)
{
maxProfit = profit;
}
}
return maxProfit;
}
};
Note:第二次scan不必存储每个状态,所以只需一个变量存储到目前为止第二次扫描的最大利润。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int size = prices.size();
int maxProfit = ;
int* profits = new int [size]; //表示从0到i的最大利润
int* profits_from_last = new int [size]; //表示从i到size-1的最大利润
//initial status
profits[] = ;
int minValue = prices[];
//scan from start
for(int i = ; i< size; i++)
{
if(prices[i] < minValue) //save the minimum value
{
minValue = prices[i];
profits[i] = profits[i-];
}
else //caculate the profit from minimum value to currentand compare to previous maximum profit(profits[i-1])
{
profits[i] = max(prices[i]-minValue,profits[i-]);
}
}
//initial status
int maxSecondProfits = ;
int maxValue = prices[size-];
//scan from last
for(int i = size-; i >= ; i--)
{
if(prices[i] > maxValue) //save the maximum value
{
maxValue = prices[i];
}
else //caculate the profit from current to maximum value and compare to previous maximum profit(maxSecondProfits)
{
maxSecondProfits = max(maxValue-prices[i],maxSecondProfits);
}
int profit = profits[i] + maxSecondProfits; //calculate profits of two transactions
if(profit>maxProfit)
{
maxProfit = profit;
}
}
return maxProfit;
}
};
法II:进一步提升space efficiency。扫描一次,从而不用存储第一次交易的状态。
在扫描的时候,把当前点看作两次交易都完成的点,计算当前最大利润。
然后再依次更新把当前点看作第二次买入点的最大利润,看作第一次卖出点的最大利润,第一次买入点的最大利润,这些都为了在下一个循环用来更新状态。
所以,需要用4个变量存储到当前日为止第1/2次买入/卖出的利润最大值。
时间复杂度O(n)
class Solution {
public:
int maxProfit(vector<int>& prices) {
int dates = prices.size();
if(dates <= ) return ;
int hold1 = INT_MIN, hold2 = INT_MIN;//buy stock
int release1 = , release2 = ;//sell stock
for(int i = ; i < dates; i++){
release2 = max(release2, hold2+prices[i]);// The maximum if we've just sold 2nd stock so far.
hold2 = max(hold2, release1 - prices[i]); // The maximum if we've just buy 2nd stock so far.
release1 = max(release1, hold1+prices[i]);// The maximum if we've just sold 1nd stock so far.
hold1 = max(hold1, -prices[i]); // The maximum if we've just buy 1st stock so far.
}
return release2;
}
};
123. Best Time to Buy and Sell Stock III (Array; DP)的更多相关文章
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- [leetcode]123. 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】123 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 123. 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 a ...
- LeetCode OJ 123. 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 ...
- 123. 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 al ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Linux下XAMPP装完之后,Navicat无法连上数据库的问题的解决 注意'mypassword'是当前的mysql登录密码
Linux下装完XAMPP之后,mysql是自带装好了的,这个时候,mysql的root用户没有密码. 在mac 下安装好xampp后,需要在终端命令行操作时,比如输入:mysql -u root - ...
- 第12章 网络基础(2)_数据封装和IP地址
4. 数据封装和IP地址 (1)数据封装 (2)IP地址 ①在TCP/IP网络中,每个主机都有唯一的地址,它是通过IP协议族实现的. ②IP协议要求在每次与TCP/IP网络建立连接时,每台主机都必须为 ...
- ubuntu18.04 安装hadoop 2.7.3+hive 2.3.4
1. 安装hadoop 详细请参见本人的另外一片博文<Hadoop 2.7.3 分布式集群安装> 2. 下载hive 2.3.4 解压文件到/opt/software -bin.tar.g ...
- Hive基础之Hive表常用操作
本案例使用的数据均来源于Oracle自带的emp和dept表 创建表 语法: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name ...
- mysql更新(七) MySQl创建用户和授权
14-补充内容:MySQl创建用户和授权 权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作 ...
- Arrays.binarySearch 数组二分查找
public static void main(String[] args) throws Exception { /** * binarySearch(Object[], Object key) a ...
- 数据分析利器之hive优化十大原则
hive之于数据民工,就如同锄头之于农民伯伯.hive用的好,才能从地里(数据库)里挖出更多的数据来. 用过hive的朋友,我想或多或少都有类似的经历:一天下来,没跑几次hive,就到下班时间了. h ...
- spring data jpa 注解
@Data 注解引出的 lombok 小辣椒 今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok, ...
- Redis 几个全局命令, 以及事物
1, 清空当前数据库的所有数据 => flushdb 2, 清空所有数据库的所有数据 => flushall 3, key 值检索命令 => scan num match if 会 ...
- J2SE 8的集合
List ArrayList查询效率高LinkedList插入删除效率高 ArrayList ArrayList<String> arrayList = new ArrayList< ...