House Robber

Moderate
0/80
Average time to solve is 35m
profile
Contributed by
10 upvotes
Asked in companies
Park+India TodayGoogle inc

Problem statement

Mr. X. is a professional robber planning to rob houses along a street. Each house has a certain amount of money hidden.

Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two neighboring houses were broken into the same night.

You are given an array/list of non-negative integers 'A' representing the amount of money hidden at each house. Your task is to return the maximum amount of money Mr. X can rob tonight without alerting the police.

Example:
Input: [1,2,3,1]

Output: 4

Mr. X. can rob the first house and the third house, total money = 1 + 3.
Hence, the total money robbed by Mr. X. is 4.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line will contain the integer 'T', denoting the number of test cases.

The first line of each test case contains one integer 'N' denoting the size of array 'A'.

The second line of each test case contains 'N' single space-separated integers, elements of the array 'A'.
Output format :
For each test case, Return the maximum amount of money Mr. X can rob tonight without alerting the police.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 50
1 <= N <= 10^5
0 <= A[i] <= 10ٰ^9

Time Limit: 1 sec
Sample Input 1 :
2
4   
4 1 6 10
5
2 5 8 9 1
Sample Output 1 :
14
14
Explanation Of Sample Input 1 :
For the first case:

Input: [4,1,6,10]

Output: 4

Mr. X. can rob the first house and the fourth house, total money = 4 + 10.
Hence, the total money robbed by Mr. X. is 14.

For the second case:

Input: [2,5,8,9,1]

Output: 14

Mr. X. can rob the second and fourth houses, total money = 5 + 9.
Hence, the total money robbed by Mr. X. is 14.
Sample Input 2 :
2
8
6 1 50 1 50 30 6 100
6
55 19 21 13 10 67
Sample Output 2 :
206
143
Hint

Check all possibilities.

Approaches (2)
Brute Force

In this approach, we are trying out all possibilities.

At every 'i'th house robber has two options 

  • Rob the current house.
  • Don't rob the current house.

If he is robbing the 'i'th house, he can have the money hidden at 'i'th house, and the money robbed till 'i-2'th house, let's say it 'X'.

If he is not robbing the 'i'th house, he can have the money robbed till 'i-1'th house, let's say it 'Y'.

So, the maximum money he robs till 'i'th house is max( X, Y ).
 

The steps are as follows:-

function houseRobberyHelper([int] A, int index):

  1. If all the houses are already covered, i.e., index >= A.size, return 0.
  2. Initialise an integer variable, 'X' to A[ index ] + houseRobberyHelper( A, index+2 ).
  3. Initialize an integer variable, 'Y' to houseRobberyHelper( A, index+1 ).
  4. Return the maximum of 'X and 'Y'.

function houseRobbery([int] A):

  1. Return houseRobberyHelper(A, 0).
Time Complexity

O( 2^N ), Where 'N' is the size of the given array 'A'.

We are trying out all possibilities, and every index has two choices.

Hence the time complexity is O( 2^N ).

Space Complexity

O( 1 )

We are using constant extra space for storing the results.

Hence space complexity is O( 1 ).

Code Solution
(100% EXP penalty)
House Robber
Full screen
Console