A professional robber is planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping him from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money he can rob tonight without alerting the police.
The problem can be cut down to sub-problem of finding whether to rob from ith house or not.
Now, in case of taking decision whether to rob from ith house or not, it completely depends on where the money maximizes. Since, adjacent houses are connected, the robber can't rob two adjacent house. Either he can rob from (i-1)th house or he can rob from ith house after robbing from (i-2)th house. To maximize the money simply find which results in maximum amount. In terms of recursive relation we can write,
This recursive relation ultimately yields to the maximum amount that can be robbed.
We can form our DP matrix and convert the recursive relation in memorization.
A 1D array where mone stashed in houses are stored. Let the array to be money
Example with explanation:
That gives the maximum amount that can be robbed without breaking the security.
Outputneed an explanation for this answer? contact us directly to get an explanation for this answer