当前位置:网站首页>LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
2022-08-01 05:30:00 【wangjun861205】
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
Example 2:
Input: prices = [1]
Output: 0
Constraints:
- 1 <= prices.length <= 5000
- 0 <= prices[i] <= 1000
当 prices[i] >= prices[i-1]的时候, 我们有两个选择, 一个是卖掉当前的股票, 然后跳到 i+2(因为卖掉之后有 1 天的冷静期), 另一个是不卖, 然后正常走到 i+1. 我们同时维护当前的最高和最低价格, 类似于一个单调递增队列, 用于记录当前的最佳买入时机和最佳卖出时机
use std::collections::HashMap;
impl Solution {
fn dp(
prices: &Vec<i32>,
i: usize,
min: i32,
max: i32,
cache: &mut HashMap<(usize, i32, i32), i32>,
) -> i32 {
if i >= prices.len() {
return max - min;
}
let curr = prices[i];
if min == -1 && max == -1 {
let ans = if let Some(c) = cache.get(&(i + 1, curr, curr)) {
*c
} else {
Solution::dp(prices, i + 1, curr, curr, cache)
};
cache.insert((i, -1, -1), ans);
return ans;
}
if curr >= max {
let sell = if let Some(c) = cache.get(&(i + 2, -1, -1)) {
*c
} else {
Solution::dp(prices, i + 2, -1, -1, cache)
} + curr
- min;
let keep = if let Some(c) = cache.get(&(i + 1, min, max)) {
*c
} else {
Solution::dp(prices, i + 1, min, max, cache)
};
let ans = sell.max(keep);
cache.insert((i, min, max), ans);
return ans;
}
if curr <= min {
let ans = if let Some(c) = cache.get(&(i + 1, curr, curr)) {
*c
} else {
Solution::dp(prices, i + 1, curr, curr, cache)
};
cache.insert((i, min, max), ans);
return ans;
} else {
let ans = if let Some(c) = cache.get(&(i + 1, min, curr)) {
*c
} else {
Solution::dp(prices, i + 1, min, curr, cache)
};
cache.insert((i, min, max), ans);
return ans;
}
}
pub fn max_profit(prices: Vec<i32>) -> i32 {
Solution::dp(&prices, 0, -1, -1, &mut HashMap::new())
}
}
边栏推荐
- 可持久化线段树
- 2022.7.26 模拟赛
- 零序电流继电器器JL-8C-12-2-2
- MySQL-DML language-database operation language-insert-update-delete-truncate
- pytroch、tensorflow对比学习—使用GPU训练模型
- Selenium:操作Cookie
- vsce package 后出现 Command failed: npm list --production --parseable --depth=99999 --loglevel=error异常
- JS的运行原理
- II. Binary tree to Offer 68 - recent common ancestor
- 解决浏览器滚动条导致的页面闪烁问题
猜你喜欢

WPF项目-初步了解数据绑定 binding

PaddleX部署推理模型和GUI界面测试结果不一致的解决方法

类神经网络训练不起来怎么办

WebSocket implements chat function
![[target detection] YOLOv7 theoretical introduction + practical test](/img/ff/a83acbf9dd5cc2f907f3538d287842.png)
[target detection] YOLOv7 theoretical introduction + practical test

(Codeforce 757) E. Bash Plays with Functions

pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)

USB3.0:VL817Q7-C0的LAYOUT指南(二)

2022年超全的Android面经(附含面试题|进阶资料)

Qt Widget 项目对qml的加载实例
随机推荐
对话MySQL之父:一个优秀程序员可抵5个普通程序员
小心你的字典和样板代码
Robot_Framework:常用内置关键字
WebSocket实现聊天功能
Selenium:元素定位
ORACLE modify another user package (package)
MySQL-DML language-database operation language-insert-update-delete-truncate
类神经网络训练不起来怎么办
Use controls as brushes to get bitmap code records
可视化全链路日志追踪
戴尔PowerEdge服务器R450 RAID配置步骤
WPF项目-按着键盘方向键,移动格子盒子效果
leetcode43 string multiplication
uva12326
AspNet.WebApi.Owin custom Token request parameters
冲刺金九银十,Android开发面试(内含面试资料|面试题|源码)
II. Binary tree to Offer 68 - recent common ancestor
零序电流继电器器JL-8C-12-2-2
uva12326
使用string 容器翻转 字母