Skip to main content
CodeOath
← All problems

Problem

House Robber

Medium
  • dynamic-programming

In a row of houses, nums[i] is the cash in house i. You may rob any set of houses as long as no two of them are next to each other. Return the largest total you can take.

Example 1
Input
nums = [4, 9, 3, 8, 6]
Output
17
Explanation

houses 1 and 3 are not neighbours, and 9 + 8 = 17. Taking every other house from the start gives only 4 + 3 + 6 = 13.

Example 2
Input
nums = [2, 1, 1, 2]
Output
4
Explanation

the first and last houses are not neighbours, so both can be robbed for 2 + 2. Skipping two houses in a row is allowed.

Constraints:

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

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.