Skip to content

Commit ce4e858

Browse files
committed
House Robber II
1 parent 43b8ac1 commit ce4e858

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

213-house-robber-ii.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/house-robber-ii/
3+
4+
You are a professional robber planning to rob houses along a street.
5+
Each house has a certain amount of money stashed. All houses at this
6+
place are arranged in a circle. That means the first house is the
7+
neighbor of the last one. Meanwhile, adjacent houses have a security
8+
system connected, and it will automatically contact the police if
9+
two adjacent houses were broken into on the same night.
10+
11+
Given an integer array nums representing the amount of money of each house,
12+
return the maximum amount of money you can rob tonight without alerting the police.
13+
14+
Example 1:
15+
Input: nums = [2,3,2]
16+
Output: 3
17+
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
18+
because they are adjacent houses.
19+
20+
Example 2:
21+
Input: nums = [1,2,3,1]
22+
Output: 4
23+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
24+
Total amount you can rob = 1 + 3 = 4.
25+
26+
Example 3:
27+
Input: nums = [1,2,3]
28+
Output: 3
29+
30+
Constraints:
31+
32+
1 <= nums.length <= 100
33+
0 <= nums[i] <= 1000
34+
"""
35+
class Solution:
36+
def rob(self, nums: List[int]) -> int:
37+
if len(nums) == 1:
38+
return nums[0]
39+
40+
sl_house = nums[0]
41+
l_house = max(nums[0], nums[1])
42+
sl_house_exculding_first = 0
43+
l_house_exculding_first = nums[1]
44+
45+
for index in range(2, len(nums)):
46+
if index < len(nums) - 1:
47+
sl_house, l_house = l_house, max(l_house, nums[index] + sl_house)
48+
49+
sl_house_exculding_first, l_house_exculding_first = l_house_exculding_first, max(l_house_exculding_first, nums[index] + sl_house_exculding_first)
50+
51+
return max(l_house, l_house_exculding_first)
52+
53+
# Two Pass
54+
class Solution1:
55+
def rob(self, nums: List[int]) -> int:
56+
if len(nums) == 1:
57+
return nums[0]
58+
59+
second_last_house = nums[0]
60+
last_house = max(nums[0], nums[1])
61+
62+
for index in range(2, len(nums)-1):
63+
second_last_house, last_house = last_house, max(last_house, nums[index] + second_last_house)
64+
65+
res = last_house
66+
# excluding first
67+
second_last_house = 0
68+
last_house = nums[1]
69+
70+
for index in range(2, len(nums)):
71+
second_last_house, last_house = last_house, max(last_house, nums[index] + second_last_house)
72+
73+
return max(res, last_house)
74+
# Memo
75+
class Solution2:
76+
def rob(self, nums: List[int]) -> int:
77+
if len(nums) == 1:
78+
return nums[0]
79+
80+
return max(self.helper(nums, len(nums)-1, False, {}), self.helper(nums, len(nums)-2, True, {}))
81+
82+
def helper(self, nums, index, include_first, memo):
83+
if index < 0 or (index == 0 and not include_first):
84+
return 0
85+
86+
if index not in memo:
87+
memo[index] = max(self.helper(nums, index-1, include_first, memo), nums[index] + self.helper(nums, index - 2, include_first, memo))
88+
89+
return memo[index]
90+
91+
92+
# TLE
93+
class Solution3:
94+
def rob(self, nums: List[int]) -> int:
95+
if len(nums) == 1:
96+
return nums[0]
97+
98+
return max(self.helper(nums, len(nums)-1), self.helper(nums, len(nums)-2, True))
99+
100+
def helper(self, nums, index, include_first=False):
101+
if index < 0 or (index == 0 and not include_first):
102+
return 0
103+
104+
return max(self.helper(nums, index-1, include_first), nums[index] + self.helper(nums, index - 2, include_first))

0 commit comments

Comments
 (0)