public int rob(int[] num) {
int n = num.length;
if (n < 2)
return n == 0 ? 0 : num[0];
int[] cache = new int[n];
cache[0] = num[0];
cache[1] = num[0] > num[1] ? num[0] : num[1];
for (int i = 2; i < n; i++) {
cache[i] = cache[i  2] + num[i];
cache[i] = cache[i] > cache[i1]? cache[i] : cache[i1];
}
return cache[n  1];
}
Java O(n) DP solution with 13 lines clean code.


0 houses return 0.
1 house returns num[0].
2 houses return whichever larger.
For n houses where n>2:
Consider the last house num[n1]. It is either robbed, or NOT robbed.
If robbed, then amount of money is (total of n2 houses) + num[n1].
If NOT robbed, then amount of money is (total of n1 houses).
Return whichever larger.