House Robber
Given an integer array nums where nums[i] is the money in the i-th house along a street, return the maximum amount you can rob. You cannot rob two adjacent houses, because adjacent houses share a connected alarm system that alerts the police.
Open official problem prompt ↗Compute the largest amount of money obtainable from a line of houses when picking any subset that contains no two neighbors.
Walking down a street of vending machines you can only trigger every other one; at each machine you decide whether skipping it or grabbing it (plus whatever you banked before its neighbor) leaves you richer.
- Input
- nums = [2, 7, 9, 3, 1]
- Output
- 12
- Why
- Robbing houses 0, 2, and 4 gives 2 + 9 + 1 = 12, and no two are adjacent.
1 <= nums.length <= 1000 <= nums[i] <= 400