首页 > 其他分享 >LeetCode: 309. Best Time to Buy and Sell Stock with Cooldown

LeetCode: 309. Best Time to Buy and Sell Stock with Cooldown

时间:2022-12-05 18:01:02浏览次数:73  
标签:Sell 309 Buy BUY int32 COOLDOWN prices SELL dp


​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 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]

解题思路 —— 动态规划

  1. ​dp[i+1][BUY]​​​ 表示前 ​​i​​​ 天进入 ​​BUY​​ 状态的最大收益
  2. ​dp[i+1][SELL]​​​ 表示前 ​​i​​​ 天进入 ​​SELL​​ 状态的最大收益
  3. ​dp[i+1][COOLDOWN]​​​ 表示前 ​​i​​​ 天进入 ​​COOLDOWN​​ 状态的最大收益

AC 代码

func max(lhv, rhv int32) int32 {
if lhv > rhv {
return lhv
} else {
return rhv
}
}

func maxProfit(prices []int) int {

const (
BUY = iota
SELL
COOLDOWN
)

dp := make([][3]int32, len(prices)+1)
dp[0][0], dp[0][1], dp[0][2] = math.MinInt32, math.MinInt32, 0

for i := 0; i < len(prices); i++ {
dp[i+1][BUY] = max(dp[i][COOLDOWN] - int32(prices[i]), dp[i][BUY])

if dp[i][BUY] == math.MinInt32 {
dp[i+1][SELL] = math.MinInt32
} else {
dp[i+1][SELL] = dp[i][BUY] + int32(prices[i])
}

dp[i+1][COOLDOWN] = max(dp[i][SELL], dp[i][COOLDOWN])

}

return int(max(dp[len(prices)][COOLDOWN], dp[len(prices)][SELL]))
}


标签:Sell,309,Buy,BUY,int32,COOLDOWN,prices,SELL,dp
From: https://blog.51cto.com/u_15903085/5913221

相关文章

  • C++ 继承和派生的应用 1.定义一个类 Book, 用来描述新书, 具有以下功能:(1) 查看当前
    Book.h:#pragmaonce#include<string>usingnamespacestd;classBook{public:Book(conststring&bookname,conststring&isbn,doubleprice);doubl......
  • 2022-2023-1 20221309《计算机基础与程序设计》第十二周学习总结
    作业信息这个作业属于哪个课程<班级的链接>这个作业要求在哪里<作业要求的链接> 这个作业的目标《C语言程序设计》第十一章https://www.cnblogs.......
  • 121. Best Time to Buy and Sell Stock
    Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,......
  • 各类数据库写入Webhsell总结
    1.MySQL写入WebShell1.1写入条件数据库的当前用户为ROOT或拥有FILE权限;知道网站目录的绝对路径;PHP的GPC参数为off状态;MySQL中的secure_file_priv参数不能为NULL状态;......
  • xhsell下载,xshell免费下载、使用、无限期
    https://www.xshell.com/zh/free-for-home-school/  然后会发邮件过来  对应点击链接即可:(注意,有期限) ......
  • P5309 [Ynoi2011] 初始化
    P5309[Ynoi2011]初始化考虑暴力,模拟题意,时间复杂度竟是\(O(\frac{n^2}{x})\),那么对于\(x\ge\sqrt{n}\)的修改就可以暴力了,这不是根号分治吗。再去考虑\(x<\sqrt{n}......
  • 题解 UVA12265【贩卖土地 Selling Land】
    postedon2022-09-2414:33:29|under题解|sourceproblem一个黑白矩阵,求以每个点为右下角,能围出的周长最大的全白矩形的周长。\(n\leq2000\)。solution试图进行......
  • 白嫖永久服务器1668309535005
    阿贝云服务器注册免费领取1核1g内存5m宽带10g内存的云服务器,对于个人来说完全够用了。还有免费备案和虚拟主机,免备案对于搭建个人博客就很方便,部署了小项目上去,运行流畅不......
  • 白嫖永久服务器1668309600001
    阿贝云服务器注册免费领取1核1g内存5m宽带10g内存的云服务器,对于个人来说完全够用了。还有免费备案和虚拟主机,免备案对于搭建个人博客就很方便,部署了小项目上去,运行流畅不......
  • 白嫖永久服务器1668309595005
    阿贝云服务器注册免费领取1核1g内存5m宽带10g内存的云服务器,对于个人来说完全够用了。还有免费备案和虚拟主机,免备案对于搭建个人博客就很方便,部署了小项目上去,运行流畅不......