LeetCode Best Time to Buy and Sell Stock with Cooldown
原题链接在这里:https://leetcode.com/problems/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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
- You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
- After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
题解:
Let buy[i] denotes maximum profit till index i, series of transactions ending with buy.
Let sell[i] denotes maimum profit till index i, series of transactions ending with sell.
There is a cooldown before buy again. buy[i] could happend after sell[i-2], not sell[i-1].
Thus, buy[i] = Math.max(buy[i-1], sell[i-2]-prices[i]).
If wanna sell a stock, must buy stock before. Thus sell[i] is calculated with buy[i-1].
sell[i] = Math.max(sell[i-1], buy[i-1]+prices[i]).
只用到了i-1, i-2两个之前的量,所以可以使用常量来代替数组.
b0 as buy[i], b1 as buy[i-1].
s0 as sell[i], s1 as sell[i-1], s2 as sell[i-2].
Time Complexity: O(n). n = prices.length.
Space: O(1).
AC Java:
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length < 2){
return 0;
} int b0 = -prices[0];
int b1 = b0;
int s2 = 0;
int s1 = 0;
int s0 = 0;
for(int i = 1; i<prices.length; i++){
b0 = Math.max(b1, s2-prices[i]);
s0 = Math.max(s1, b1+prices[i]); b1 = b0;
s2 = s1;
s1 = s0;
} return s0;
}
}
LeetCode Best Time to Buy and Sell Stock with Cooldown的更多相关文章
- [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 ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
- [LeetCode] Best Time to Buy and Sell Stock 6道合集【DP】
1. Best Time to Buy and Sell Stock 2. Best Time to Buy and Sell Stock II 3. Best Time to Buy and Sel ...
随机推荐
- topcoder SRM 618 DIV2 MovingRooksDiv2
一开始Y1,Y2两个参数看不懂,再看一遍题目后才知道,vector<int>索引代表是行数,值代表的是列 此题数据量不大,直接深度搜索即可 注意这里深度搜索的访问标识不是以前的索引和元素, ...
- ACM 房间安排
房间安排 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 2010年上海世界博览会(Expo2010),是第41届世界博览会.于2010年5月1日至10月31日期间, ...
- 优化特性(Attribute)性能
通过这篇文章,不仅可以了解到Attribute的工作原理,还可以了解到GetcustomeAttribute是的内部执行流程.最后,你会看到,使用缓存机制可以极大的优化反射Attribute的性能. ...
- 4分钟apache自带ab压力测试工具使用: 2015.10.4
2015.10.44分钟apache自带ab压力测试工具使用:win8.1 wampserver2.5 -Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b 可以参考一下部 ...
- 【HDU】4336 Card Collector
http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意:n张卡片,每一次取一个盒子,盒子里装有卡片i的概率是p[i],求得到所有卡片所需要开的盒子的期望数( ...
- 【HDU】1846 Brave Game
http://acm.hdu.edu.cn/showproblem.php?pid=1846 题意:二人博弈,1堆石子每次取1~m个,没有石子可取的输,输出先手胜利还是后手胜利. #include & ...
- Window对象简介
Window对象是JavaScript层级中的顶层对象. Window对象表示一个浏览器窗口或一个框架,它在<body>或<frameset>出现时被自动创建. Window对 ...
- (转)FTP操作类,从FTP下载文件
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- mysql配置之skip-external-locking
转载:http://www.kuqin.com/database/20120815/328905.html MySQL的配置文件my.cnf中默认存在一行skip-external-locking的参 ...
- MySQL数据库基本数据类型
1.整型 2. 浮点数类型和定点数类型 3.日期与时间类型 4.字符串类型 5. 二进制类型