I knew this problem but still fumbled explaining the recurrence out loud.
Recognize this as the classic House Robber problem and immediately frame it as a dynamic programming problem. Define the state as the maximum amount robbed up to house i, then derive the recurrence dp[i] = max(dp[i-1], dp[i-2] + nums[i]). Optimize space to O(1) by keeping only the last two values.
Pro tip: Mention that this problem is equivalent to finding a maximum weight independent set on a path graph, which shows deeper algorithmic insight. Also, explicitly discuss edge cases like empty array, single house, and all zeros to demonstrate thoroughness.
Restate the problem to ensure understanding: non-negative integers, cannot rob adjacent houses, maximize sum. Ask about constraints (e.g., array size, values) if not given.
Let dp[i] be the max amount from first i houses. Recurrence: dp[i] = max(dp[i-1], dp[i-2] + nums[i]). Base cases: dp[0]=0, dp[1]=nums[0].
Observe that only dp[i-1] and dp[i-2] are needed. Use two variables to achieve O(1) space while maintaining O(n) time.
Check for empty array (return 0), single house (return its value), and all zeros (return 0). Ensure code handles these gracefully.
State time O(n) and space O(1). Walk through a small example (e.g., [2,7,9,3,1]) to verify recurrence and edge cases.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.