题目:

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 buyint[] sell = new int[len + 1];      // before i, for any sequence last action at i is going to be sellint[] cooldown = new int[len + 1];  // before i, for any sequence last action at i is going to be cooldownbuy[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 profitsell[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 buyint[] s1 = new int[len];                // to sellint[] s2 = new int[len];                // to rests0[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相关推荐

  1. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

  2. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)

    [LeetCode]309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: https: ...

  3. 309. Best Time to Buy and Sell Stock with Cooldown**

    309. Best Time to Buy and Sell Stock with Cooldown** https://leetcode.com/problems/best-time-to-buy- ...

  4. [刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown

    题目 代码 class Solution {public int maxProfit(int[] prices) {if (prices.length <= 1) return 0;int l ...

  5. 【leetcode-Python】-Dynamic Programming -309. Best Time to Buy and Sell Stock with Cooldown

    题目链接 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目描述 给定价格数组prices, ...

  6. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. [leetcode] 309. 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 ar ...

  8. 【Java力扣算法】LeetCode 309 Best Time to Buy and Sell Stock with Cooldown(动态规划)

    题目: 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 .​ 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔交 ...

  9. 309.Best Time to Buy and Sell Stock with Cooldown

    1.基本思路: 此题可通过DP来解决,但状态转移方程的构想不是那么明显,首先可以通过两个数组来表示买与卖的信息: buy[i]:表示第i天为止,最近一次股票购入后所持有的利润 sell[i]:表示第i ...

最新文章

  1. YII2中分页组件的使用
  2. 【游记】NOIP 2017
  3. details在Java中_Spring安全性在Java配置中使用两个自定义UserDetailsS​​ervices
  4. tensorflow gpu利用率为0_训练效率低?GPU利用率上不去?快来看看别人家的tricks吧...
  5. 操作系统 第二章【进程管理】【MOOC答案】
  6. 在Outlook 2010中使用对话视图
  7. Ant Desgin Pro 修改登录后默认导航到的位置
  8. bmon:一个强大的网络带宽监视和调试工具
  9. vscode android入门,vscode Android调试
  10. 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、和运算符重载...
  11. ffmpeg php centos,Centos安装ffmpeg插件
  12. java 静态内部类 内部类_Java中内部类和静态内部类的区别
  13. 人脸识别-YOLOv5模型目标检测
  14. AIX系统怎么更换服务器硬盘(mirrorvg)
  15. Word怎么在空白处添加下划线
  16. 恩尼格玛模拟器_使用ENIGMA模拟器进行数据加密和解密
  17. 苹果删除照片不释放内存_原来苹果手机这样清理内存,可以释放大量空间,真是太好用了...
  18. 阿里云CDN是什么?阿里云CDN配置与购买优惠教程
  19. mysql数据库结构导出word_Windows导出MySQL数据库表结构到Word文档-DBExportDoc V1.0 For MySQL...
  20. idea30天免费试用,看看到底好不好用

热门文章

  1. View类的xml属性和相关方法说明
  2. 表达式树 php,Linux_LINQ学习笔记:表达式树,构建查询表达式 本节中, 我们 - phpStudy...
  3. 信息论与编码_哈夫曼编码
  4. vue component created没有触发_Vue 全局数据管理-Vuex
  5. iis php 访问乱码,如何解决php加密 乱码问题
  6. android调用c闪退未看到log,解决:Android开发 看不到崩溃的日志
  7. 使用GPG校验sign签名
  8. Golang结构体struct的使用(结构体嵌套, 匿名结构体等)
  9. mysql创建用户,并赋予权限:只能查某个数据库中的某张表(只读)
  10. Android禁止ViewPager的左右滑动