Loading…
Loading…
Given an array of non-negative integers representing money stashed in houses along a street, find the maximum amount you can rob without robbing two adjacent houses (doing so trips the alarm).
Input: 1 2 3 1
Output: 4 (rob house 1 and house 3: 1 + 3 = 4)
At each house, you either skip it (carry forward the best total so far) or rob it (best total from two houses back, plus this house's value). Track just the previous two running totals instead of a full array — O(n) time, O(1) space.
Line 1: the array, space-separated
Print the maximum amount.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.