<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are <strong>arranged in a circle.</strong> That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and&nbsp;<b>it will automatically contact the police if two adjacent houses were broken into on the same night</b>.</p>

<p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <strong>without alerting the police</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 100</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

class Solution:
    def calculate(self, nums: List[int]) -> int:
        if (len(nums) == 0):
            return 0
        if (len(nums) == 1):
            return nums[0]
        if (len(nums) == 2):
            return max(nums)
        if (len(nums) == 3):
            return max(nums[1], nums[0] + nums[2])
        nums[2] += nums[0]
        for i in range(3, len(nums)):
            nums[i] = max(nums[i] + nums[i - 2], nums[i] + nums[i - 3])
        return max(nums[len(nums) - 1], nums[len(nums) - 2])

    def rob(self, nums: List[int]) -> int:
        if (len(nums) == 1):
            return nums[0]
        first = self.calculate(nums[:-1])
        last = self.calculate(nums[1:])
        print(first, last)
        return max(first, last)




        # # Edge case: num has three or less elements
        # if (len(nums) <= 3):
        #     return max(nums)
        
        # # Set up ref
        # ref = []
        # ref.append((nums[0], True))
        # ref.append((nums[1], False))
        # ref.append((nums[0] + nums[2], True))
        
        # for i in range(3, len(nums) - 1):
        #     # val: total amount of money
        #     # flag: whether house 1 is robbed
        #     if (nums[i] + ref[i - 2][0] > nums[i] + ref[i - 3][0]):
        #         val = nums[i] + ref[i - 2][0]
        #         flag = ref[i - 2][1]
        #     elif (nums[i] + ref[i - 2][0] == nums[i] + ref[i - 3][0]):
        #         val = nums[i] + ref[i - 2][0]
        #         if (not ref[i - 2][1]):
        #             flag = ref[i - 2][1]
        #         else:
        #             flag = ref[i - 3][1]
        #     else:
        #         val = nums[i] + ref[i - 3][0]
        #         flag = ref[i - 3][1]
        #     ref.append((val, flag))
        
        # # Last house
        # i = len(nums) - 1
        # if (ref[i - 2][1]):
        #     ref2_val = ref[i - 2][0] - nums[0]
        # else:
        #     ref2_val = ref[i - 2][0]
        # if (ref[i - 3][1]):
        #     ref3_val = ref[i - 3][0] - nums[0]
        # else:
        #     ref3_val = ref[i - 3][0]
        # if (nums[i] + ref2_val > nums[i] + ref3_val):
        #     val = nums[i] + ref2_val
        # else:
        #     val = nums[i] + ref3_val
        # flag = False
        # ref.append((val, flag))

        # print(ref)

        # return max(ref[i][0], ref[i - 1][0], ref[i - 2][0])
16400