当前位置:网站首页>LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
2022-08-01 05:48: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]的时候, 我们有两个选择, One is to sell the current stock, 然后跳到 i+2(because after the sale 1 天的冷静期), The other is not for sale, then walk normally i+1. We maintain both the current maximum and minimum prices, Similar to a monotonically increasing queue, Used to record the current best buying time and best selling time
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())
}
}
边栏推荐
猜你喜欢
Robot_Framework:断言
第5章——以程序方式处理MySQL数据表的数据
【FiddlerScript】利用FiddlerScript抓包保利威下载
滚动条样式修改
NDK does not contain any platforms问题解决
Windows taskbar icon abnormal solution
移动应用恶意攻击激增500% 三六零天御为APP免费构建安全屏障
奇葩问题 npm install 报错 gyp ERR
LeetCode 0149. Maximum number of points on a line
2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)
随机推荐
可视化全链路日志追踪
Talk about the bugs in using for in to traverse the array in js
Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
Selenium:鼠标、键盘事件
小白的0基础教程SQL: 什么是SQL 01
微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
2022.7.27 Selected lectures on good topics
vim configuration + ctag is as easy to read code as source insight
Causes and solutions of lock table
响应式织梦模板园林景观类网站
Robot_Framework:关键字
Selenium:操作Cookie
Using FiddlerScript caught poly FiddlerScript 】 【 download
小心你的字典和样板代码
企业员工人事管理系统(数据库课设)
将CSV文件快速导入MySQL中
说说js中使用for in遍历数组存在的bug
曲柄滑块机构运动分析和参数优化
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
MySQL-Data Definition Language-DDLdatebase define language