FastPrepHouse Robber
Problem · Array

House Robber

Learn this problem
MediumSalesforce logoSalesforceFULLTIMEONSITE INTERVIEW
See Salesforce hiring insights

Problem statement

You are given an array nums where nums[i] is the amount available in house i. The houses form one line.

Select houses to maximize the total amount, subject to the rule that two adjacent houses cannot both be selected. Return the maximum total amount.

Function

rob(nums: int[]) → int

Examples

Example 1

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

Select houses 0 and 2 for a total of 1 + 3 = 4.

Example 2

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

Select houses 0, 2, and 4 for a total of 12.

Example 3

nums = [5]return = 5

The only house can be selected.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^4
  • The maximum total fits in a signed 32-bit integer.

More Salesforce problems

drafts saved locally
public int rob(int[] nums) {
    // write your code here
}
nums[1,2,3,1]
expected4
checking account