Advertisement

Best Time to Buy and Sell Stock with Cooldown - LeetCode 309 Solution

Best Time to Buy and Sell Stock with Cooldown - Complete Solution Guide

Best Time to Buy and Sell Stock with Cooldown is LeetCode problem 309, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array prices where prices[i] is the price of a given stock on the i th 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:

Detailed Explanation

The problem asks us to find the maximum profit we can make by buying and selling a stock multiple times, given an array of stock prices for each day. However, there's a cooldown period of one day after each sale, meaning you cannot buy stock on the day immediately following a sale. You also cannot engage in multiple transactions simultaneously, implying you must sell before you buy again. The input is an array `prices` representing the stock price on each day, and the output is the maximum profit achievable.

Solution Approach

The solution uses dynamic programming with state compression to achieve O(1) space complexity. Instead of using arrays to store the maximum profit for buy, sell, and cooldown states at each day, it uses three variables `buy_prev`, `sell_prev`, and `sell_prev_prev` to store the maximum profit for these states at the previous and two previous days. This approach works by iteratively updating these variables as we traverse the `prices` array. The `buy_curr` and `sell_curr` are computed based on previous values ensuring the cooldown constraint is respected.

Step-by-Step Algorithm

  1. Step 1: Initialize `buy_prev` to negative infinity (Integer.MIN_VALUE in Java/C++) because initially, we cannot have any profit if we buy the stock on the first day. Initialize `sell_prev` and `sell_prev_prev` to 0 because if we don't buy or sell anything, the profit is 0.
  2. Step 2: Iterate through the `prices` array, where each `price` represents the stock price on a given day.
  3. Step 3: Calculate `buy_curr`. The maximum profit when buying on the current day is the maximum of: a) the profit from buying on the previous day (`buy_prev`), and b) the profit from selling two days ago minus the current price (`sell_prev_prev - price`). This ensures the cooldown period is enforced, as we're buying after a cooldown.
  4. Step 4: Calculate `sell_curr`. The maximum profit when selling on the current day is the maximum of: a) the profit from selling on the previous day (`sell_prev`), and b) the profit from buying on the previous day plus the current price (`buy_prev + price`).
  5. Step 5: Update the variables for the next iteration. `sell_prev_prev` becomes `sell_prev`, `sell_prev` becomes `sell_curr`, and `buy_prev` becomes `buy_curr`.
  6. Step 6: After iterating through all prices, the final `sell_prev` will contain the maximum profit we can achieve. Return `sell_prev`.

Key Insights

  • Insight 1: Dynamic Programming is the appropriate technique to solve this optimization problem, allowing us to build up the solution from subproblems.
  • Insight 2: We need to track three states for each day: the maximum profit if we 'buy' on that day, the maximum profit if we 'sell' on that day, and the maximum profit if we are in a 'cooldown' state.
  • Insight 3: Because of the cooldown, the 'buy' state on a particular day depends on the 'sell' state from *two* days prior, not just the previous day.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Dynamic Programming.

Companies

Asked at: Geico, Google, Visa.