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 Java
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int price : 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 minimum price seen so far.
2fill in blank
mediumComplete the code to calculate the maximum profit with unlimited transactions.
DSA Java
public int maxProfit(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 with prices[i] instead of prices[i - 1].
Using prices[i + 1] which causes index errors.
✗ Incorrect
We compare the current price with the previous day's price to decide if we can make a profit.
3fill in blank
hardFix the error in the code to find max profit with at most two transactions.
DSA Java
public int maxProfit(int[] prices) {
int n = prices.length;
int[] leftProfits = 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];
int maxProfit = leftProfits[n - 1];
int rightProfit = 0;
for (int i = n - 2; i >= 0; i--) {
maxPrice = Math.max(maxPrice, prices[i]);
rightProfit = Math.max(rightProfit, maxPrice - prices[i]);
maxProfit = Math.max(maxProfit, rightProfit + leftProfits[i]);
}
return maxProfit;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using maxPrice instead of minPrice in leftProfits calculation.
Using prices[i] instead of minPrice.
✗ Incorrect
We calculate profit by subtracting the minimum price seen so far from the current price.
4fill in blank
hardFill both blanks to implement max profit with cooldown period.
DSA Java
public int maxProfit(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;
buy[1] = Math.max(-prices[0], -prices[1]);
sell[1] = Math.max(0, prices[1] - prices[0]);
for (int i = 2; i < n; i++) {
buy[i] = Math.max(buy[i - 1], sell[i - 2] - [1]);
sell[i] = Math.max(sell[i - 1], buy[i - 1] + [2]);
}
return sell[n - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prices[i - 1] or prices[i + 1] causing index errors.
Mixing buy and sell prices incorrectly.
✗ Incorrect
We subtract the current price when buying and add the current price when selling.
5fill in blank
hardFill all three blanks to implement max profit with at most k transactions.
DSA Java
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (n == 0) return 0;
if (k >= n / 2) {
int profit = 0;
for (int i = 1; i < n; i++) {
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
int[][] dp = new int[k + 1][n];
for (int i = 1; i <= k; i++) {
int maxDiff = -prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j - 1], prices[j] + [1]);
maxDiff = Math.max(maxDiff, dp[i - 1][j - 1] - [2]);
}
}
return dp[k][n - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[i][j - 1] instead of maxDiff in dp update.
Mixing indices in maxDiff update.
✗ Incorrect
We use maxDiff to track the best balance and prices[j] for current price in calculations.
