Unit 12 Session 1 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE
Input:
prices = [1, 2, 3, 0, 2]
Output:
3
Explanation:
Gary should buy on day 1, sell on day 2, rest on day 3, and buy on day 4 and sell on day 5.
EDGE CASE
Input:
prices = [1]
Output:
0
Explanation:
With only one day, no trades can be made.
Match what this problem looks like to known categories of problems, e.g. Arrays or Dynamic Programming, and strategies or patterns in those categories.
For Trading Problems with Cooldown, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use dynamic programming to simulate the state transitions. We maintain three DP arrays: buy, sell, and cooldown to keep track of the maximum profit for each day based on whether Gary is buying, selling, or resting.
Initialization:
prices array is empty or has only one day, return 0.buy[i] tracks the maximum profit if Gary buys on day i.sell[i] tracks the maximum profit if Gary sells on day i.cooldown[i] tracks the maximum profit if Gary is resting on day i.Base Case:
buy[0] = -prices[0]sell[0] = 0cooldown[0] = 0State Transitions:
i from 1 to n - 1, compute:
buy[i]: The maximum profit if Gary buys on day i, either by buying today or keeping the previous buy.sell[i]: The maximum profit if Gary sells on day i, either by selling today or keeping the previous sell.cooldown[i]: The maximum profit if Gary rests today, either by continuing the previous cooldown or resting after selling.Return the Result:
sell or cooldown state.Implement the code to solve the algorithm.
def max_pokedollar_profit(prices):
if not prices:
return 0
n = len(prices)
if n == 1:
return 0
# Initialize the DP arrays
buy = [-float('inf')] * n
sell = [0] * n
cooldown = [0] * n
# Base case: First day transactions
buy[0] = -prices[0] # Buying on the first day
for i in range(1, n):
# Transitions for each day
buy[i] = max(buy[i - 1], cooldown[i - 1] - prices[i]) # Buy on day i or keep previous buy
sell[i] = max(sell[i - 1], buy[i - 1] + prices[i]) # Sell on day i or keep previous sell
cooldown[i] = max(cooldown[i - 1], sell[i - 1]) # Cooldown after selling
# The maximum profit will be in the sell or cooldown state on the last day
return max(sell[-1], cooldown[-1])
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
prices = [1, 2, 3, 0, 2]
3
Example 2:
prices = [1]
0
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the number of days.
O(n) because we compute the state transitions for each day.O(n) for storing the buy, sell, and cooldown arrays.