Loot Houses
A thief wants to loot houses. He knows the amount of money in each house. He cannot loot two consecutive houses. Find the maximum amount of money he can loot.
Input Format :
The first line of input contains a single integer 'N' denoting the total number of houses.
The second line of input contains 'N' single space-separated integers, denoting the amount of money in every 'i-th' house.
Output Format :
The only line of output will print the maximum amount of loot that is possible.
Note :
You don’t need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
0 <= 'N' <= 10^5
0 <= 'A[i]' <= 10^4
Where 'A[i]' represents the money present in the 'i-th' house.
Time limit: 1 sec
CodingNinjas
author
2y
Recursive Approach
Suppose that the robber is at the ith house. The robber has two options:
- If he decides to rob this house, then he cannot rob the next house, so he’ll have to go to the house after tha...read more
CodingNinjas
author
2y
Dynamic Programming Approach
We can use dynamic programming to optimize our recursive approach. We store the computed value of maxLoot(i) in a dp array, and we do this for all i between 1 to N. In this...read more
CodingNinjas
author
2y
DP Space Optimised Approach
Consider building the result array in a bottom-up fashion (computing the result for smaller to larger numbers of houses). We can observe that if we’re at the ith house, we o...read more
Add answer anonymously...
Top Internshala Web Developer interview questions & answers
Popular interview questions of Web Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app