Amazon Interview Question: House Robber Problem (Max Non-Adjacent Sum | DP)

19 Views
No Comments

You are given an array of integers representing the amount of money stored in each house.
The houses are arranged in a line, and you cannot rob two adjacent houses.

Return the maximum amount of money you can rob without ever robbing two adjacent houses.

Example

Input:  [5, 1, 2, 6, 20, 2]
Output: 27          # 5 + 6 + 20 = 31? → Actually the optimal is 5 + 2 + 20 = 27

(Explanation: choose non-adjacent houses so the sum is maximized.)


Use dynamic programming.
For each index i, compute dp[i] = max(dp[i-1], dp[i-2] + nums[i]).
Final answer is dp[n-1].

The VOprep team has long accompanied candidates through various major company OAs and VOs, including Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

END
 0