Easy Solution in Python with 9 Lines


  • 2
    P
    class Solution(object):
        def rob(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if len(nums) == 1:
                return nums[0]
            m_low = m_high = 0
            for n in nums:            # first round
                m_high, m_low = max(m_low + n, m_high), m_high
            first_round = m_high
    
            for n in nums:            # second round
                m_high, m_low = max(m_low + n, m_high), m_high
            return m_high - first_round
    

    The problem can be treated equivalent as the problem House Robber I, but the input is a list from the list joined with a copy of itself. (1,2,...,n,1,2,..n). and just return the money from the second half.


  • 0
    W

    Could you please explain why the second half would be the answer? Appreciate it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.