Skip to main content
All Questions

Given n houses in a line with money in each, find the maximum amount a robber can steal, without stealing from two adjacent houses.

Medium
You are given an array representing the value in the houses e.g. [100, 20, 40, 70, 80]. Return 100 + 40 + 80 = 220

You're planning to rob houses along a street. House i holds nums[i] in cash, and neighboring houses share an alarm system: robbing two adjacent houses on the same night alerts the police.

Return the most money you can take without robbing two adjacent houses.

Example 1

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

Rob house 0 (1) and house 2 (3).

Example 2

Input: nums = [2, 7, 9, 3, 1] Output: 12

Rob houses 0, 2 and 4: 2 + 9 + 1 = 12.

Constraints

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

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.