What is the main goal in the 'Buy and Sell Stocks' problem?
To maximize profit by choosing the best days to buy and sell stocks given their prices over time.
Click to reveal answer
beginner
In the single transaction variant, what is the key approach to find maximum profit?
Track the minimum price seen so far and calculate the profit if sold today; update max profit accordingly.
Click to reveal answer
intermediate
How does the unlimited transactions variant differ from the single transaction variant?
You can buy and sell multiple times, so you sum all positive differences between consecutive days' prices.
Click to reveal answer
advanced
What is the role of dynamic programming in the 'Buy and Sell Stocks with cooldown' variant?
It helps track states like holding, sold, and cooldown to maximize profit while respecting cooldown rules.
Click to reveal answer
advanced
Why do we use a 2D DP array in the 'Buy and Sell Stocks with transaction fee' variant?
To track profits with and without holding a stock on each day while accounting for transaction fees.
Click to reveal answer
In the single transaction stock problem, what do you track to find max profit?
ANumber of transactions
BMaximum price so far only
CSum of all prices
DMinimum price so far and max profit
✗ Incorrect
Tracking the minimum price so far allows calculating potential profit if sold today, updating max profit.
For unlimited transactions, how do you calculate total profit?
AIgnore price changes
BSum of all positive price differences between consecutive days
COnly one transaction profit
DDifference between max and min price overall
✗ Incorrect
You add all positive gains from each day to the next to maximize profit with unlimited buys and sells.
What does cooldown mean in stock trading problems?
AYou must wait one day after selling before buying again
BYou can buy and sell anytime
CYou cannot sell stocks
DYou must buy every day
✗ Incorrect
Cooldown means after selling, you cannot buy the next day, so you must wait one day.
In the transaction fee variant, when is the fee applied?
AEvery day regardless
BWhen you buy a stock
CWhen you sell a stock
DNo fee applied
✗ Incorrect
The fee is subtracted from the profit when you sell a stock.
Which data structure is commonly used to solve complex stock variants with constraints?
ADynamic Programming arrays
BLinked lists
CStacks
DBinary trees
✗ Incorrect
Dynamic Programming arrays help track states and optimize profit under constraints.
Explain how to find maximum profit in the single transaction stock problem.
Think about the lowest price before the current day.
You got /3 concepts.
Describe how cooldown affects buying and selling decisions in stock trading problems.
Consider what happens right after you sell.
You got /3 concepts.
Practice
(1/5)
1. What is the main goal when solving the Buy and Sell Stocks problem variants?
easy
A. Maximize the profit by choosing the best days to buy and sell stocks
B. Minimize the number of transactions regardless of profit
C. Find the stock price on the last day
D. Calculate the average stock price over all days
Solution
Step 1: Understand the problem goal
The problem asks to maximize profit by buying and selling stocks on certain days.
Step 2: Identify the key objective
Maximizing profit means buying low and selling high at the best possible times.
Final Answer:
Maximize the profit by choosing the best days to buy and sell stocks -> Option A
Quick Check:
Goal = Maximize profit [OK]
Hint: Focus on profit maximization, not just transactions [OK]
Common Mistakes:
Confusing profit maximization with minimizing transactions
Ignoring the buy before sell rule
Thinking the goal is to find average price
2. Which of the following Rust code snippets correctly initializes a vector of stock prices for the Buy and Sell Stocks problem?
easy
A. let prices = {7, 1, 5, 3, 6, 4};
B. let prices = vec![7, 1, 5, 3, 6, 4];
C. let prices = (7, 1, 5, 3, 6, 4);
D. let prices = [7, 1, 5, 3, 6, 4];
Solution
Step 1: Identify Rust vector syntax
In Rust, vectors are created using vec![...] macro.
Step 2: Check each option
let prices = vec![7, 1, 5, 3, 6, 4]; uses vec![...], which is correct. let prices = [7, 1, 5, 3, 6, 4]; is an array, not a vector. let prices = (7, 1, 5, 3, 6, 4); is a tuple. let prices = {7, 1, 5, 3, 6, 4}; is invalid syntax.
Final Answer:
let prices = vec![7, 1, 5, 3, 6, 4]; -> Option B
Quick Check:
Vector = vec![...] syntax [OK]
Hint: Use vec![] to create vectors in Rust [OK]
Common Mistakes:
Using array syntax instead of vector
Using tuple instead of vector
Invalid curly braces for initialization
3. What is the output of the following Rust code that solves the 'Best Time to Buy and Sell Stock I' problem?
fn max_profit(prices: Vec<i32>) -> i32 {
let mut min_price = i32::MAX;
let mut max_profit = 0;
for price in prices {
if price < min_price {
min_price = price;
} else if price - min_price > max_profit {
max_profit = price - min_price;
}
}
max_profit
}
fn main() {
let prices = vec![7, 1, 5, 3, 6, 4];
println!("{}", max_profit(prices));
}
medium
A. 7
B. 6
C. 4
D. 5
Solution
Step 1: Trace the loop to find min_price and max_profit