#213

House Robber II

specialist · 940 · lc medium +32 · verified · 44.6% accepted · 11,005 likes · top 27%

play →

Description

Houses sit in a circle, so the first and last are neighbors. A shared alarm fires if any two adjacent houses are burglarized the same night.

Given an integer array nums with the cash in each house, return the most money you can steal without setting off the alarm.

Example 1:

Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.

Example 2:

Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

Example 3:

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

Code

1
2
3