House Robber

Implement houseRobber

A thief is casing a street of houses, each holding a known amount of cash, arranged in a straight line. Every house is wired to its immediate neighbors by a shared security system — robbing two adjacent houses on the same night trips the alarm — but skipping around freely otherwise. Given the amount of cash in each house, find the maximum total that can be robbed in one night without ever robbing two neighboring houses. The key idea is that every house boils down to a single yes/no decision: rob it or leave it. Leaving it means the best possible total is whatever could already be made starting from the next house. Robbing it means banking this house's cash and jumping two houses ahead, since the very next one is now off-limits. Whichever of those two choices yields more is the best possible outcome from that point onward — and because this same two-way choice repeats at every house, it can be solved by sliding through the street once, tracking only the best totals seen at the two most recently considered houses.

Example 1:

Input: nums = [3,8,4,9,6]

Output: 17

Example 2:

Input: nums = [6,2,5,3,9]

Output: 20

Example 3:

Input: nums = [9,1,1,9]

Output: 18

+ 7 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 400

nums =

[3, 8, 4, 9, 6]