0
0
DSA Cprogramming~10 mins

Buy and Sell Stocks All Variants in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Buy and Sell Stocks All Variants
Start with prices array
Choose variant: single, multiple, cooldown, fee
Initialize variables: buy, sell, cooldown, fee
Iterate over prices
Update buy/sell states based on variant rules
At end, return max profit
Done
The flow starts with the prices array, selects the variant, updates buy/sell states per day, and ends with the max profit.
Execution Sample
DSA C
int maxProfit(int* prices, int n) {
  int buy = -prices[0], sell = 0;
  for (int i = 1; i < n; i++) {
    buy = buy > -prices[i] ? buy : -prices[i];
    sell = sell > buy + prices[i] ? sell : buy + prices[i];
  }
  return sell;
}
This code calculates max profit for single transaction by tracking buy and sell states.
Execution Table
StepOperationDay (i)PriceBuy StateSell StateActionProfit So Far
0Initialize07-70Buy at price 70
1Iterate11-10Update buy: max(-7,-1)=-10
2Iterate25-14Update sell: max(0,-1+5)=44
3Iterate33-14No change buy or sell4
4Iterate46-15Update sell: max(4,-1+6)=55
5Iterate54-15No change buy or sell5
6End---15Return sell as max profit5
💡 Reached end of prices array, max profit is sell state = 5
Variable Tracker
VariableStartAfter Day 1After Day 2After Day 3After Day 4After Day 5Final
buy-7-1-1-1-1-1-1
sell0044555
Key Moments - 3 Insights
Why does buy state become -1 on day 1 instead of staying -7?
On day 1, buy is updated to max(-7, -1) = -1, meaning buying at price 1 is better than previous buy at 7 (see execution_table row 1).
Why does sell state not change on day 3 even though price changes?
On day 3, sell remains 4 because max(4, -1 + 3) = 4, so selling at price 3 doesn't improve profit (see execution_table row 3).
Why return sell state as max profit at the end?
Sell state tracks max profit after selling, so at end it holds max profit achievable (see execution_table row 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the buy state at day 2?
A-1
B-7
C0
D4
💡 Hint
Check the 'Buy State' column at Step 2 in execution_table.
At which day does the sell state first increase above 0?
ADay 1
BDay 2
CDay 3
DDay 4
💡 Hint
Look at 'Sell State' column in execution_table rows for days 1 to 4.
If price on day 4 was 7 instead of 6, how would sell state change at day 4?
ARemain 5
BIncrease to 7
CIncrease to 6
DDecrease below 5
💡 Hint
Calculate max(sell at day 3, buy + new price) using variable_tracker values.
Concept Snapshot
Buy and Sell Stocks Variants:
- Track buy and sell states per day
- buy = max(buy, -price)
- sell = max(sell, buy + price)
- Variants add cooldown, fees, or multiple transactions
- Return sell state as max profit
- Update states iteratively over prices array
Full Transcript
This visualization shows how to find max profit from stock prices by tracking buy and sell states daily. We start by buying at the first price, then update buy and sell states each day to reflect best decisions. Buy state means best cost to hold stock, sell state means best profit after selling. We update buy as max of previous buy or buying today after selling. Sell updates as max of previous sell or selling today after buying. At the end, sell state holds max profit. The example uses prices [7,1,5,3,6,4] and shows step-by-step updates. Key moments clarify why buy changes on day 1 and why sell stays same on day 3. Quiz questions test understanding of state changes and effects of price changes.