Back

House Robber

medium

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Test Cases

Copy an input into the main harness and Run to verify
Input
nums = [1,2,3,1]
Expected Output
4

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

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

Explanation: Rob house 0 (2), house 2 (9), and house 4 (1).

Constraints

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

Hints

Hint 1 — click to reveal

At each house you decide: rob it (skip the previous) or skip it.

Hint 2 — click to reveal

dp[i] = max(dp[i-1], dp[i-2] + nums[i]).

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.