Loading problem…
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. The only constraint stopping you from robbing each of them is that adjacent houses have security systems connected — if two adjacent houses were broken into on the same night, the police will be automatically contacted.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
This is a classic 1D dynamic programming problem. At each house, you decide: rob it (skip the previous) or skip it (keep the previous best).
rob([1, 2, 3, 1]); // 4 (rob house 0 and 2: 1+3=4)
rob([2, 7, 9, 3, 1]); // 12 (rob house 0, 2, 4: 2+9+1=12)