Skip to content
BASA DSA

House Robber

Medium1-D Dynamic Programming

Choose a subset of houses on a line without choosing adjacent houses. Return the greatest sum of their nonnegative values.

Example 1

Input
nums = [3,2,5,1]
Output
8

Explanation

Choose nonadjacent houses 0 and 2 for 3+5=8.

Example 2

Input
nums = [0,0]
Output
0

Explanation

All values are zero, so the maximum is zero.

Example 3

Input
nums = [9]
Output
9

Explanation

Take the only house for 9.

Constraints

  • 1 <= len(nums) <= 100
  • Values are 0..400

Function contractrobArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…