Skip to content
CodeKruPro

Largest total with no two neighbours (house robber)

Coding problemEasy-MediumAsked in: Adobe, Amazon, Flipkart, Google, Paytm, Walmart

A row of houses holds the amounts of money given in the list. You may take the money from any houses you like, but never from two neighbouring houses. Return the largest total you can take.

An empty row gives 0.

Examples

Example 1
Input: houses = [1,2,3,1]
Output: 4
Take houses 1 and 3: 1 + 3 = 4.
Example 2
Input: houses = [2,7,9,3,1]
Output: 12
2 + 9 + 1 = 12.
Example 3
Input: houses = [2,1,1,2]
Output: 4
Take the first and the last house. Taking every other house is not always best.

Constraints

  • The list holds between 0 and 100,000 values
  • Each amount is between 0 and 10,000

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.