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 Go
func maxProfit(prices []int) int {
minPrice := prices[0]
maxProfit := 0
for i := 1; i < len(prices); i++ {
if prices[i] < [1] {
minPrice = prices[i]
} else if prices[i]-minPrice > maxProfit {
maxProfit = prices[i] - 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 maximum profit with unlimited transactions.
DSA Go
func maxProfit(prices []int) int {
profit := 0
for i := 1; i < len(prices); 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] instead of prices[i-1].
Adding profit when prices decrease.
✗ Incorrect
We add profit when today's price is greater than yesterday's price.
3fill in blank
hardFix the error in the code to calculate max profit with at most two transactions.
DSA Go
func maxProfit(prices []int) int {
if len(prices) == 0 {
return 0
}
firstBuy, firstSell := -prices[0], 0
secondBuy, secondSell := -prices[0], 0
for i := 1; i < len(prices); i++ {
firstBuy = max(firstBuy, -prices[i])
firstSell = max(firstSell, firstBuy + prices[i])
secondBuy = max(secondBuy, firstSell - [1])
secondSell = max(secondSell, secondBuy + prices[i])
}
return secondSell
}
func max(a, b int) int {
if a > b {
return a
}
return b
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prices[0] instead of prices[i] in secondBuy update.
Mixing variables in the calculation.
✗ Incorrect
secondBuy should consider the profit after firstSell minus current price prices[i].
4fill in blank
hardFill both blanks to implement max profit with cooldown period after selling.
DSA Go
func maxProfit(prices []int) int {
n := len(prices)
if n <= 1 {
return 0
}
sell, prevSell, buy := 0, 0, -prices[0]
for i := 1; i < n; i++ {
prevBuy := buy
buy = max(buy, [1] - prices[i])
[2] = max(sell, prevBuy + prices[i])
prevSell = sell
sell = [2]
}
return sell
}
func max(a, b int) int {
if a > b {
return a
}
return b
}Attempts:
3 left
💡 Hint
Common Mistakes
Using sell instead of prevSell in buy update.
Confusing prevBuy and buy in sell update.
✗ Incorrect
buy depends on prevSell minus price; sell depends on max of previous sell and prevBuy plus price.
5fill in blank
hardFill all three blanks to implement max profit with transaction fee.
DSA Go
func maxProfit(prices []int, fee int) int {
n := len(prices)
if n == 0 {
return 0
}
cash, hold := 0, -prices[0]
for i := 1; i < n; i++ {
cash = max(cash, hold + prices[i] - [1])
hold = max(hold, cash - [2])
}
return cash
}
func max(a, b int) int {
if a > b {
return a
}
return b
}Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting fee when buying instead of selling.
Mixing price and fee in wrong places.
✗ Incorrect
When selling, subtract fee; when buying, subtract current price.
