Problem description

A row of houses each hold a certain amount of money, given in an integer array nums. A security system links every pair of directly adjacent houses, so robbing two neighboring houses on the same night triggers an alarm.

Return the largest total amount of money you can steal in one night without ever robbing two adjacent houses.

Example 1

Input: nums = [1,2,3,1]

Output: 4

Rob house 0 (money = 1) and house 2 (money = 3) for a total of 1 + 3 = 4.

Example 2

Input: nums = [2,7,9,3,1]

Output: 12

Rob houses 0, 2, and 4 (2 + 9 + 1 = 12); no two chosen houses are adjacent.

Constraints

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

After the pattern

Now solve one you haven't seen.

Tower Activation Credits is our own problem on the technique behind House Robber: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free