You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you 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 you can rob tonight without alerting the police.
Dynamic Programming problem.
For n houses, let OPT(n) denotes the maximum amount we can rob, let Vi denotes the money that the ith house holds.
For the nth house, we have two cases, rob or not rob.
- rob： OPT(n) = Vn + OPT(n - 2)
- not rob： OPT(n) = OPT(n - 1)
It means that if we rob the nth house, we can only rob the (n-2)th house next, if we don’t rob the nth house, then we can rob the (n-1)th house next.
n means the total number of houses.
- n = 0，total amount we can rob is 0
- n = 1，total amount equals to the money the first house holds
- n = 2，total amount equals to the larger amount of the first and the second house holds