当前位置:网站首页>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())
}
}
边栏推荐
猜你喜欢
随机推荐
类神经网络训练不起来怎么办
点餐系统数据库设计--SQL Server
Selenium: element judgment
轻量级的VsCode为何越用越大?为什么吃了我C盘10G?如何无痛清理VsCode缓存?手把手教你为C盘瘦身
Using FiddlerScript caught poly FiddlerScript 】 【 download
About making a progress bar for software initialization for Qt
flinkcdc对mysql的date字段类型转化有什么解决思路么
crypto-js使用
Qt Widget project loading example of qml
Robot growth in China
第6章——数据库的安全性
Robot_Framework: commonly used built-in keywords
NUMPY
仿牛客网项目总结
Win任务栏图标异常解决
Selenium:元素判断
Check控件
LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
A,H,K,N