"""
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.
"""


def max_robbed_naive(houses, start=0, cache=None):
    if len(houses) == 0:
        return 0

    if cache is None:
        cache = {}

    if start in cache:
        return cache[start]

    if start + 2 >= len(houses):
        val = max(houses[start:])
    else:
        val = max(houses[start] + max((max_robbed_naive(houses, i, cache) for i in xrange(start + 2, len(houses)))),
                  max((max_robbed_naive(houses, i, cache) for i in xrange(start + 1, len(houses)))))
    cache[start] = val
    return val


def max_robbed(houses, lo=0, hi=None):
    hi = hi or len(houses)
    prev_yes, prev_no = 0, 0
    for i in xrange(lo, hi):
        prev_no, prev_yes = max(prev_yes, prev_no), houses[i] + prev_no
    return max(prev_yes, prev_no)


def circular_max_robbed(houses):
    if len(houses) == 1:
        return houses[0]

    return max(max_robbed(houses, 0, len(houses) - 1), max_robbed(houses, 1, len(houses)))


if __name__ == '__main__':
    print max_robbed([1, 2])
    print max_robbed([1, 1, 1])
    print max_robbed([5, 1, 2, 6, 3])
    print max_robbed([1, 5, 2, 6, 3, 8, 1, 9, 3, 2, 3, 6])
    print max_robbed([2, 3, 2])

    print circular_max_robbed([1, 1, 1])
