198. House Robber

Problem:

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.

Classical Dynamic Programming Problem. So, figure out the state transition equation.

\begin{eqnarray}r(i)=
\begin{cases}
nums_0, & i = 0 \cr
max(nums_0, nums_1), & i = 1 \cr
max(r_{i-2}+nums_i, r_{i-1}), & i >= 2
\end{cases}
\end{eqnarray}

Clearly Code:

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        robs = []
        maxRob = 0
        for i in xrange(len(nums)):
            if i < 2:
                maxRob = nums[i] if nums[i] > maxRob else maxRob
                robs.append(maxRob)
            else:
                tmp = max(robs[i-2]+nums[i], robs[i-1])
                robs.append(tmp)
                maxRob = tmp if tmp > maxRob else maxRob
        return maxRob

Three Line Solution:

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        last, now = 0, 0
        for i in nums: last, now = now, max(last + i, now)
        return now

One Line Solution:

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return reduce(lambda (_1st, _2nd), i: (_2nd, max(i + _1st, _2nd)),nums,(0,0)) [1]

213. House Robber II

Problem:

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

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.

Now it's a loop, so the first house and the last house can not be visited once. So once visit except the first house, once the last. Compare the two visits and choose the max.

Use the above one line solution twice:

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        elif len(nums) == 1:
            return nums[0]
        firRound = reduce(lambda (_1st, _2nd), i: (_2nd, max(i + _1st, _2nd)), nums[:-1], (0,0)) [1]
        secRound = reduce(lambda (_1st, _2nd), i: (_2nd, max(i + _1st, _2nd)), nums[1:], (0,0)) [1]
        return max(firRound, secRound)

337. House Robber III

Continue ...