Facebook Pixel

You are a robber planning to rob houses along a street. Each house has a certain amount of treasure stashed, the only constraint stopping you from robbing every one of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Input & Output
Input
nums — the amount of money stashed in each house, in street order
Output
the maximum amount of money that can be robbed without breaking into two adjacent houses
Example
Input
nums = [1, 2, 3, 1]
Output
4
Explanation

Rob house 0 and house 2 for 1 + 3 = 4. Houses 2 and 3 are adjacent, so the two largest values cannot both be taken.

Example
Input
nums = [2, 7, 9, 3, 1]
Output
12
Explanation

Rob houses 0, 2, and 4 for 2 + 9 + 1 = 12. Taking the two largest values that are not adjacent, 7 and 3, yields only 10, so picking greedily by value is not enough.

Constraints
  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 400

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro