1 题目理解

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:

Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

2 动态规划

class Solution {public int maxProfit(int[] prices) {if(prices == null || prices.length == 0) return 0;int n = prices.length;int[][] dp = new int[n][3];dp[0][0] = -prices[0];for(int i=1;i<n;i++){dp[i][0] = Math.max(dp[i-1][2] - prices[i], dp[i-1][0]);dp[i][1] = dp[i-1][0] + prices[i];dp[i][2] = Math.max(dp[i-1][2],dp[i-1][1]);}return Math.max(dp[n-1][1],dp[n-1][2]);}
}

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. 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 ...

  8. [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 ...

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

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

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

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

最新文章

  1. js 常用的正则表达式
  2. myeclipse和maven的clean和build
  3. P6805-[CEOI2020]春季大扫除【贪心,树链剖分,线段树】
  4. jquery对象和DOM对象的相互转换详解
  5. LeetCode 958. 二叉树的完全性检验(层序遍历)
  6. 想成为企业争抢的目标吗?你需要掌握的五大热门IT技能
  7. 倩女幽魂服务器维护时间,9月5日在线维护公告
  8. SQL开头quoted和ansiNULL
  9. 【java】java 多线程 异步计算 FutureTask 源码详解
  10. 老板亲戚来公司实习,差点把公司的Tomcat搞崩了,就因为不会部署Java服务
  11. python连接hive kerberos_数据库开发实战教程:使用Python连接Kerberos的Presto
  12. C语言冒泡排序(函数版)
  13. 像电影里黑客高手一样写代码
  14. Python 微信自动化工具开发系列06_根据用户信息自动回复升级版本(2022年10月可用)
  15. 后缀为axd 的文件
  16. 一个程序员分享8年的开发经验
  17. VS2015 + QT 编译出现public: virtual struct QMetaObject const * __thiscall 错误
  18. Vultr IP及SSH 故障排除信息
  19. 百度搜索技术沙龙感悟
  20. 128Echarts - 关系图(NPM Dependencies)

热门文章

  1. 关于CaciiEZ端口流量阀值报警的设置
  2. 单片机的 FIFO循环队列实现
  3. Linux与Windows中动态链接库的分析与对比
  4. 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加...
  5. PHP中过滤数组中的元素
  6. maya计算机内存不足请保存,Maya内存不足不能渲染怎么优化场景?
  7. 华水c语言课程设计,【图片】发几个C语言课程设计源代码(恭喜自己当上技术小吧主)【东华理工大学吧】_百度贴吧...
  8. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)
  9. typescript 接口 interface 的理解
  10. ubuntu等linux系统给windows共享文件