CodePark/ problems / No Two Neighbouring Houses
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Dynamic programming

No Two Neighbouring Houses

mediumabout 20 minutes in an interview

The houses stand in a row, and houses[i] is how much money is in house i. Choose some houses to collect from. There is one rule: you may never pick two houses that are next to each other.

Return the biggest total you can collect.

  • houses has between 0 and 1,000 entries.
  • Each entry is a whole number between 0 and 1,000.
  • No houses means a total of 0.
Input: houses = [3, 10, 4, 2, 8] Output: 18Take the houses at index 1 and 4: 10 + 8. Taking 3 + 4 + 8 = 15 is less.
Input: houses = [2, 1, 1, 2] Output: 4Take the first and last houses.
Input: houses = [] Output: 0