0
0
DSA Typescriptprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Buy and Sell Stocks All Variants
Start with price array
Choose variant: single, multiple, cooldown, fee
Initialize variables: buy, sell, cooldown, fee
Iterate over prices
Update buy/sell states based on rules
Track max profit
End iteration
Return max profit
The flow starts with the price list, selects the variant rule, iterates over prices updating buy/sell states, and finally returns the max profit.
Execution Sample
DSA Typescript
const prices = [7,1,5,3,6,4];
let buy = -Infinity;
let sell = 0;
for (const price of prices) {
  buy = Math.max(buy, -price);
  sell = Math.max(sell, buy + price);
}
console.log(sell);
This code finds max profit for single buy-sell transaction by tracking best buy and sell states.
Execution Table
StepOperationPricebuy (max profit after buy)sell (max profit after sell)Visual State
1Initialize--Infinity0buy=-∞, sell=0
2Price=77max(-∞, -7) = -7max(0, -7+7) = 0buy=-7, sell=0
3Price=11max(-7, -1) = -1max(0, -1+1) = 0buy=-1, sell=0
4Price=55max(-1, -5) = -1max(0, -1+5) = 4buy=-1, sell=4
5Price=33max(-1, -3) = -1max(4, -1+3) = 4buy=-1, sell=4
6Price=66max(-1, -6) = -1max(4, -1+6) = 5buy=-1, sell=5
7Price=44max(-1, -4) = -1max(5, -1+4) = 5buy=-1, sell=5
8End---Max profit = 5
💡 All prices processed, max profit is sell = 5
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
buy-Infinity-7-1-1-1-1-1-1
sell00044555
Key Moments - 3 Insights
Why do we initialize buy with -Infinity?
We start with buy = -Infinity to ensure any price will update buy to a valid negative value representing cost. See execution_table row 1 and 2 where buy changes from -Infinity to -7.
Why does sell never decrease?
sell tracks max profit after selling, so it only updates if a better profit is found. It never decreases because we use max() function. See execution_table rows 3 to 7 where sell stays or increases.
Why do we update buy with max(buy, -price)?
buy represents the best cost to buy so far (negative price). max(buy, -price) means we keep the best (highest) buy value, which is the least cost. See execution_table rows 2 to 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the value of sell?
A0
B-1
C4
D5
💡 Hint
Check the 'sell' column in execution_table row 4.
At which step does buy first update from -Infinity?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'buy' column in execution_table rows 1 and 2.
If prices were all increasing, how would sell change?
Asell would increase steadily
Bsell would decrease
Csell would stay 0
Dsell would fluctuate
💡 Hint
Refer to how sell updates in execution_table when price increases.
Concept Snapshot
Buy and Sell Stocks Variants:
- Track buy (max profit after buy) and sell (max profit after sell)
- For single transaction: buy = max(buy, -price), sell = max(sell, buy + price)
- For multiple transactions, cooldown, or fee, update states accordingly
- Return sell as max profit
- Use iteration over prices to update states
Full Transcript
This visualization shows how to find the maximum profit from buying and selling stocks in different variants. We start with a price list and initialize buy to negative infinity and sell to zero. We iterate over each price, updating buy to the maximum of current buy or negative current price, representing the best buying cost. We update sell to the maximum of current sell or buy plus current price, representing the best profit after selling. The execution table tracks these values step-by-step. Key moments clarify why buy starts at negative infinity, why sell never decreases, and why buy updates with max(buy, -price). The quiz tests understanding of these updates. The concept snapshot summarizes the approach for all variants.