Menu
CoddyTech

House Robber

Houses stand in a row along a street, and nums[i] is the money in house i. You may take the money from any houses you choose, but never from two houses that stand next to each other. Return the largest total you can take.

Function

rob(nums: integer-array) → integer
numsinteger-array
the money in each house, in street order
Returnsinteger
the largest total you can take without taking from two adjacent houses

Constraints

  • 1 ≤ nums.length ≤ 104
  • 0 ≤ nums[i] ≤ 1000
  • The answer is at most 5 × 106, so it fits in a signed 32-bit integer.

Examples

Input
nums = [5, 3, 4, 11, 2]
Output
16
Explanation
Take 5 and 11 from houses 0 and 3 for 16. Skipping two houses in a row is allowed, and here it beats every other plan: 5 + 4 + 2 = 11 and 3 + 11 = 14.
challenge icon

Follow-up

Return the houses to take as well as the total. What must you keep from the table to rebuild that list, and can two running totals still do it?

Reset code
def rob(nums):
    # Write code here

Test #1

Test #2

Test #3

Argument

nums = [5, 3, 4, 11, 2]

Expected Output

16