309. 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]
链接: http://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
题解:
股票题又来啦,这应该是目前股票系列的最后一题。卖出之后有cooldown,然后求multi transaction的最大profit。第一印象就是dp,但每次dp的题目,转移方程怎么也写不好,一定要好好加强。出这道题的dietpepsi在discuss里也写了他的思路和解法,大家都去看一看。不过我自己没看懂....dp功力太差了, 反而是后面有一个哥们的state machine解法比较说得通。上面解法是based on一天只有一个操作,或买或卖或hold。也有一些理解为可以当天买卖的解法,列在了discuss里。看来题目真的要指定得非常仔细,否则读明白都很困难。
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0) {
return 0;
}
int len = prices.length;
int[] buy = new int[len + 1]; // before i, for any sequence last action at i is going to be buy
int[] sell = new int[len + 1]; // before i, for any sequence last action at i is going to be sell
int[] cooldown = new int[len + 1]; // before i, for any sequence last action at i is going to be cooldown
buy[0] = Integer.MIN_VALUE; for(int i = 1; i < len + 1; i++) {
buy[i] = Math.max(buy[i - 1], cooldown[i - 1] - prices[i - 1]); // must sell to get profit
sell[i] = Math.max(buy[i - 1] + prices[i - 1], sell[i - 1]);
cooldown[i] = Math.max(sell[i - 1], Math.max(buy[i - 1], cooldown[i - 1]));
} return Math.max(buy[len], Math.max(sell[len], cooldown[len]));
}
}
使用State machine的
public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length < 2) {
return 0;
}
int len = prices.length;
int[] s0 = new int[len]; // to buy
int[] s1 = new int[len]; // to sell
int[] s2 = new int[len]; // to rest
s0[0] = 0;
s1[0] = -prices[0];
s2[0] = 0; for(int i = 1; i < len; i++) {
s0[i] = Math.max(s0[i - 1], s2[i - 1]);
s1[i] = Math.max(s1[i - 1], s0[i - 1] - prices[i]);
s2[i] = s1[i - 1] + prices[i];
} return Math.max(s0[len - 1], s2[len - 1]); // hold and res
}
}
有机会还要简化space complexity, 要看一看yavinci的解析。
题外话:
今天刚发现leetcode提交解答的页面加上了题目号,方便了不少,以前只是总目录有题号。 这题跟我的情况很像。现在公司的policy是买入股票必须hold 30天,而且不可以买地产类股票...我觉得自己也没接触什么数据,就做不了短线,真的很亏..
Reference:
https://leetcode.com/discuss/72030/share-my-dp-solution-by-state-machine-thinking
http://fujiaozhu.me/?p=725
http://bookshadow.com/weblog/2015/11/24/leetcode-best-time-to-buy-and-sell-stock-with-cooldown/
https://leetcode.com/discuss/71391/easiest-java-solution-with-explanations
http://www.cnblogs.com/grandyang/p/4997417.html
https://leetcode.com/discuss/71246/line-constant-space-complexity-solution-added-explanation
https://leetcode.com/discuss/73617/7-line-java-only-consider-sell-and-cooldown
https://leetcode.com/discuss/71354/share-my-thinking-process
309. Best Time to Buy and Sell Stock with Cooldown的更多相关文章
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [LeetCode] 309. 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】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票
121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】309. 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 a ...
- Leetcode - 309. 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笔记(一)309. 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. ...
- 309 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 alg ...
随机推荐
- 转载 SQL Server 2008 R2 事务与隔离级别实例讲解
原文:http://blog.itpub.net/13651903/viewspace-1082730/ 一.事务简介 SQL Server的6个隔离级别中有5个是用于隔离事务的,它们因而被称作事务隔 ...
- JAVA开发CHECK STYLE
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...
- Mac下safari、chrome打开开发者工具快捷键
mac下safari和chrome打开开发者工具的快捷键相同,都是option(alt)+command+i 这个是我的默认配置,没有更改过的.
- appium 调试问题--UiAutomator died while responding to command
运行程序问题: 解决办法: 手机系统版本较低导致,我是V4.2.2,在android 4.3 系统上运行正常 代码如下: #coding=utf-8 ''' 作者:xxx 功能:测试计算器基本功能 注 ...
- Python 文件读和写
- ionic 运行过程中动态切换API服务器地址
ionic 运行过程中动态切换API服务器地址 keywords: ionic,phonegap,cordova,网络制式,动态切换,变更,API,服务器地址,$resource,localstora ...
- Ubuntu 下编译安装linux
1. 准备工作切换为管理员权限,sudo –i 输入用户密码 进入root 权限apt-get install build-essential kernel-package libncurses5-d ...
- zoj 1450 Minimal Circle 最小覆盖圆
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=450 You are to write a program to fi ...
- javascript中继承(二)-----借用构造函数继承的个人理解
本人目录如下: 零.寒暄&回顾 一,借用构造函数 二.事件代理 三,call和apply的用法 四.总结 零.寒暄&回顾 上次博客跟大家分享了自己对原型链继承的理解,想看的同学欢迎猛击 ...
- JavaScript之常用方法讲解
1.indexOf() 定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,frominde ...