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 arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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.

Example 1:


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


Example 2:


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

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Data structures

Starter code: class Solution:
    def rob(self, nums: List[int]) -> int:
        

Solutions:
class Solution:

	def rob(self, nums):
		if not nums:
			return 0
		if len(nums) == 1:
			return nums[0]
		return max(self.helper(nums[1:]), self.helper(nums[:-1]))

	def helper(self, nums):
		now = prev = 0
		for nxt in nums:
			(now, prev) = (max(nxt + prev, now), now)
		return now


class Solution:

	def rob(self, nums):
		if len(nums) == 1:
			return nums[0]
		(last, now) = (0, 0)
		for i in nums[:-1]:
			(last, now) = (now, max(last + i, now))
		ret = now
		(last, now) = (0, 0)
		for i in nums[1:]:
			(last, now) = (now, max(last + i, now))
		return max(ret, now)


class Solution:

	def rob(self, nums):
		if not nums:
			return 0
		if len(nums) == 1:
			return nums[0]
		if len(nums) <= 3:
			return max(nums)

		def rob_line(lst):
			(last, now) = (0, 0)
			for i in lst:
				(last, now) = (now, max(now, last + i))
			return now
		return max(rob_line(nums[:-1]), rob_line(nums[1:]))


class Solution:

	def helper(self, nums, cache={}):
		if len(nums) == 0:
			return 0
		key = str(nums)
		if key in cache:
			return cache[key]
		cache[key] = max(nums[0] + self.helper(nums[2:]), self.helper(nums[1:]))
		return cache[key]

	def rob(self, nums):
		if len(nums) == 0:
			return 0
		return max(nums[0] + self.helper(nums[2:-1]), nums[-1] + self.helper(nums[1:-2]), self.helper(nums[1:-1]))


class Solution:

	def rob(self, nums):
		l = len(nums)
		if l == 0:
			return 0
		if l == 1:
			return nums[0]
		if l == 2:
			return max(nums)
		dp1 = [None] * l
		dp2 = [None] * l
		dp1[0] = nums[0]
		dp1[1] = max(nums[0], nums[1])
		for i in range(2, l - 1):
			dp1[i] = max(dp1[i - 2] + nums[i], dp1[i - 1])
		print(dp1)
		dp2[1] = nums[1]
		dp2[2] = max(nums[1], nums[2])
		for i in range(3, l):
			dp2[i] = max(dp2[i - 2] + nums[i], dp2[i - 1])
		print(dp2)
		return max(dp1[l - 2], dp2[l - 1])


class Solution:

	def rob(self, nums):
		mem = {}

		def max_money(start, rob_first=False):
			if start >= len(nums):
				return 0
			if start == len(nums) - 1 and rob_first:
				return 0
			if (start, rob_first) in mem:
				return mem[start, rob_first]
			if start == 0:
				mem[start, rob_first] = max(max_money(start + 1, False), nums[start] + max_money(start + 2, True))
			else:
				mem[start, rob_first] = max(max_money(start + 1, rob_first), nums[start] + max_money(start + 2, rob_first))
			return mem[start, rob_first]
		return max_money(0)


class Solution:

	def rob(self, nums):
		if not nums:
			return 0
		ymax = [nums[i] for i in range(len(nums))]
		nmax = [nums[i] for i in range(len(nums))]
		nmax[0] = 0
		if len(nums) > 1:
			ymax[1] = max(ymax[0], ymax[1])
		for i in range(2, len(nums)):
			if i == len(nums) - 1:
				ymax[i] = ymax[i - 1]
			else:
				ymax[i] = max(ymax[i - 1], ymax[i - 2] + ymax[i])
			nmax[i] = max(nmax[i - 1], nmax[i - 2] + nmax[i])
		return max(nmax[-1], ymax[-1])


class Solution:

	def rob(self, nums):
		if len(nums) == 0:
			return 0
		if len(nums) <= 2:
			return max(nums[0], nums[-1])
		a = [0 for _ in range(len(nums) - 1)]
		b = a.copy()
		c = b.copy()
		a[0] = nums[0]
		b[0] = nums[-1]
		for i in range(1, len(nums) - 1):
			a[i] = max(a[i - 1], a[i - 2] + nums[i])
			b[i] = max(b[i - 1], b[i - 2] + nums[-i - 1])
		return max(a[-1], b[-1])


class Solution:

	def rob_prev(self, nums):
		n = len(nums)
		if n == 0:
			return 0
		elif n < 3:
			return max(nums)
		mem = [0 for x in range(n + 1)]
		mem[0] = nums[0]
		mem[1] = max(nums[1], nums[0])
		for i in range(2, n):
			mem[i] = max(mem[i - 2] + nums[i], mem[i - 1])
		return mem[n - 1]

	def rob(self, nums):
		n = len(nums)
		if n == 0:
			return 0
		elif n < 3:
			return max(nums)
		return max(self.rob_prev(nums[1:]), self.rob_prev(nums[:len(nums) - 1]))


class Solution:

	def rob(self, nums):
		if not nums:
			return 0
		if len(nums) == 1:
			return nums[0]

		def helper(nums):
			steal = 0
			cool = 0
			for num in nums:
				(steal, cool) = (cool, max(cool, steal + num))
			return max(steal, cool)
		return max(helper(nums[:-1]), helper(nums[1:]))


class Solution:

	def rob(self, nums):

		def helper(nums):
			if not nums:
				return 0
			dp = [0] * (len(nums) + 1)
			dp[1] = nums[0]
			for i in range(2, len(nums) + 1):
				dp[i] = max(dp[i - 1], nums[i - 1] + dp[i - 2])
			return dp[-1]
		if not nums:
			return 0
		if len(nums) == 1:
			return nums[0]
		return max(helper(nums[:len(nums) - 1]), nums[-1] + helper(nums[1:len(nums) - 2]))


class Solution:

	def rob(self, nums):
		if not nums:
			return 0
		mark = [0 for i in range(len(nums))]
		result = list(mark)
		if len(nums) == 1:
			return nums[0]
		if len(nums) == 2:
			return max(nums[0], nums[1])
		mark[0] = 1
		result[0] = nums[0]
		if nums[0] > nums[1]:
			mark[1] = 1
			result[1] = nums[0]
		else:
			result[1] = nums[1]
		for i in range(2, len(nums)):
			result[i] = max(nums[i] + result[i - 2], result[i - 1])
			if nums[i] + result[i - 2] > result[i - 1]:
				mark[i] = mark[i - 2]
			else:
				mark[i] = mark[i - 1]
		if mark[0] == 1 and mark[-1] == 1:
			return max(self.solve(nums[1:]), self.solve(nums[:-1]))
		return result[-1]

	def solve(self, nums):
		if not nums:
			return 0
		if len(nums) == 1:
			return nums[0]
		if len(nums) == 2:
			return max(nums[0], nums[1])
		result = [0 for i in range(len(nums))]
		result[0] = nums[0]
		result[1] = max(nums[0], nums[1])
		for i in range(2, len(nums)):
			result[i] = max(nums[i] + result[i - 2], result[i - 1])
		return result[-1]