FastPrepHouse Robber

House Robber

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

You are given an array nums, where nums[i] is the amount of money in house i.

Adjacent houses cannot both be robbed. Return the maximum total amount that can be robbed without choosing two adjacent houses.

Function

rob(nums: int[]) → int

Examples

Example 1

nums = [1,2,3,1]return = 4

Rob houses 0 and 2 for 1 + 3 = 4.

Example 2

nums = [2,7,9,3,1]return = 12

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

Example 3

nums = [5]return = 5

The only house can be robbed.

Constraints

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

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int rob(int[] nums) {
    // Write your code here.
}
nums[1,2,3,1]
expected4
Checking account…