Practice
[2, 3, 2]?
def rob_linear(nums, start, end):
prev, curr = 0, 0
for i in range(start, end + 1):
prev, curr = curr, max(curr, prev + nums[i])
return curr
def rob(nums):
n = len(nums)
if n == 1:
return nums[0]
return max(rob_linear(nums, 0, n - 2), rob_linear(nums, 1, n - 1))
print(rob([2,3,2]))
Solution
Step 1: Trace rob_linear(nums, 0, 1)
nums[0]=2, nums[1]=3 Iteration i=0: prev=0, curr=0 -> prev=0, curr=max(0,0+2)=2 Iteration i=1: prev=2, curr=max(2,0+3)=3 Return 3Step 2: Trace rob_linear(nums, 1, 2)
nums[1]=3, nums[2]=2 Iteration i=1: prev=0, curr=0 -> prev=0, curr=max(0,0+3)=3 Iteration i=2: prev=3, curr=max(3,0+2)=3 Return 3 Max(3,3) = 3Final Answer:
Option C -> Option CQuick Check:
Both cases yield 3, final output is 3 [OK]
- Off-by-one in loop range
- Confusing which subarray to use
- Incorrect max calculation
Solution
Step 1: Understand problem constraints
The problem requires maximizing sum without robbing adjacent houses, which is a classic overlapping subproblems scenario.Step 2: Identify suitable algorithm
Dynamic programming fits because it explores choices at each house (rob or skip) and uses previous results to build optimal solution.Final Answer:
Option A -> Option AQuick Check:
Greedy fails on cases like [2,7,9,3,1], DP finds max 12 [OK]
- Greedy approach fails on non-adjacent constraints
Solution
Step 1: Understand cooldown removal effect
Without cooldown, no need to track cooldown state; buying can happen immediately after selling.Step 2: Adjust DP states
Remove 'cool' state; hold updated as max(hold, free - prices[i]); free updated as max(free, free) (effectively unchanged).Step 3: Why other options fail
Keeping 'cool' or using greedy breaks correctness or adds unnecessary complexity.Final Answer:
Option D -> Option DQuick Check:
Removing cooldown state matches classic Best Time II problem [OK]
- Trying to keep cooldown state unnecessarily
- Using greedy which fails on some inputs
Solution
Step 1: Understand the variant
Multiple transactions per day means multiple buys and sells can happen on the same day, which breaks the original assumption of one transaction per day.Step 2: Analyze solution impact
The original DP assumes at most one buy or sell per day; to handle multiple transactions per day, the DP must consider all possible transaction counts per day, increasing complexity to O(n²).Step 3: Evaluate options
Simply removing fees or allowing buys while holding leads to invalid states or incorrect profits. Only enumerating all transactions per day correctly models the problem.Final Answer:
Option B -> Option BQuick Check:
Handling multiple transactions per day requires more complex DP with nested loops [OK]
- Removing fees incorrectly
- Allowing invalid states by buying while holding
Solution
Step 1: Understand problem change
Allowing unlimited reuse means we can rob the same house multiple times, similar to unbounded knapsack.Step 2: Identify correct DP formulation
Unbounded knapsack style DP updates dp[i] considering dp[i - 1] + nums[i] repeatedly until stable.Step 3: Evaluate options
Use unbounded knapsack approach: dp[i] = max(dp[i], dp[i - 1] + nums[i]) iterating until no improvement correctly describes iterative updates until no improvement, matching unbounded knapsack logic.Final Answer:
Option B -> Option BQuick Check:
Original adjacency constraints no longer apply; unbounded knapsack DP fits [OK]
- Trying to reuse original adjacency DP
- Ignoring repeated state updates
