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.
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.
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.
1 <= T <= 50
1 <= N <= 10^5
0 <= A[i] <= 10ٰ^9
Time Limit: 1 sec
2
4
4 1 6 10
5
2 5 8 9 1
14
14
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.
2
8
6 1 50 1 50 30 6 100
6
55 19 21 13 10 67
206
143
Check all possibilities.
In this approach, we are trying out all possibilities.
At every 'i'th house robber has two options
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):
function houseRobbery([int] A):
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 ).
O( 1 )
We are using constant extra space for storing the results.
Hence space complexity is O( 1 ).