• LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)


    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())
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    批量剪辑视频怎么做?附保姆级教程,新手小白也能3分钟50+短视频。
    docker 生成镜像的几个问题
    04 Python MyQR 两行代码生成专属二维码自定义内容
    SpringCloud01
    Dubbo —— 分布式基础 Dubbo 框架入门 —— 快速上手使用 整合 SpringBoot 进行开发 —— 实用案例 —— 以及原理初探
    决策树完成图片分类任务
    ChatGPT Plus遇到订阅被拒原因与解决方案
    API的接口定义与实现过程是怎样的?
    使用TF-IDF对文本集中的单篇文本制作词云
    Boss Rush (二分答案 + 状压DP)
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126082634