Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum profit from a single buy and sell.
DSA C#
public int MaxProfit(int[] prices) {
int minPrice = int.MaxValue;
int maxProfit = 0;
foreach (int price in prices) {
if (price < [1]) {
minPrice = price;
} else if (price - minPrice > maxProfit) {
maxProfit = price - minPrice;
}
}
return maxProfit;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using maxProfit instead of minPrice in the comparison.
Not updating minPrice correctly.
✗ Incorrect
We update minPrice when the current price is less than the recorded minPrice to track the lowest buying price.
2fill in blank
mediumComplete the code to calculate maximum profit with unlimited transactions.
DSA C#
public int MaxProfitUnlimited(int[] prices) {
int profit = 0;
for (int i = 1; i < prices.Length; i++) {
if (prices[i] > [1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing prices[i] with prices[i] (same day).
Using prices[i + 1] which can cause index out of range.
✗ Incorrect
We compare the current price with the previous day's price to decide if we can make a profit by buying yesterday and selling today.
3fill in blank
hardFix the error in the code to calculate max profit with at most two transactions.
DSA C#
public int MaxProfitTwoTransactions(int[] prices) {
int n = prices.Length;
int[] leftProfits = new int[n];
int[] rightProfits = new int[n];
int minPrice = prices[0];
for (int i = 1; i < n; i++) {
minPrice = Math.Min(minPrice, prices[i]);
leftProfits[i] = Math.Max(leftProfits[i - 1], prices[i] - [1]);
}
int maxPrice = prices[n - 1];
for (int i = n - 2; i >= 0; i--) {
maxPrice = Math.Max(maxPrice, prices[i]);
rightProfits[i] = Math.Max(rightProfits[i + 1], maxPrice - prices[i]);
}
int maxProfit = 0;
for (int i = 0; i < n; i++) {
maxProfit = Math.Max(maxProfit, leftProfits[i] + rightProfits[i]);
}
return maxProfit;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prices[i] instead of minPrice in subtraction.
Using maxPrice in the left profits calculation.
✗ Incorrect
We subtract the minimum price seen so far from the current price to calculate profit for the left side.
4fill in blank
hardFill both blanks to implement max profit with cooldown period after selling.
DSA C#
public int MaxProfitWithCooldown(int[] prices) {
int n = prices.Length;
if (n <= 1) return 0;
int[] sell = new int[n];
int[] buy = new int[n];
buy[0] = -prices[0];
sell[0] = 0;
for (int i = 1; i < n; i++) {
buy[i] = Math.Max(buy[i - 1], [1] - prices[i]);
sell[i] = Math.Max(sell[i - 1], buy[i - 1] + [2]);
}
return sell[n - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using sell[i - 1] instead of sell[i - 2] for cooldown.
Incorrectly adding prices in sell calculation.
✗ Incorrect
For buy, we consider the sell state two days before due to cooldown. For sell, we add current price to previous buy.
5fill in blank
hardFill all three blanks to implement max profit with transaction fee.
DSA C#
public int MaxProfitWithFee(int[] prices, int fee) {
int n = prices.Length;
int cash = 0;
int hold = -prices[0];
for (int i = 1; i < n; i++) {
cash = Math.Max(cash, hold + [1] - [2]);
hold = Math.Max(hold, cash - [3]);
}
return cash;
}Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting fee when buying instead of selling.
Using fee + 1 which is incorrect.
✗ Incorrect
When selling, subtract fee from prices[i]. When buying, subtract prices[i] from cash.
