Skip to content

Commit bcbb7f8

Browse files
committed
- Solution for "House Robber #264".
1 parent 91e2247 commit bcbb7f8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

house-robber/ayosecu.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(nums)
6+
- Space Complexity: O(n), n = len(nums)
7+
"""
8+
def rob(self, nums: List[int]) -> int:
9+
# DP Formation
10+
# money[0] = 0
11+
# money[1] = nums[0]
12+
# money[i+1] = max(money[i-1] + nums[i], money[i])
13+
14+
money = [0] * (len(nums) + 1)
15+
money[1] = nums[0]
16+
for i in range(1, len(nums)):
17+
money[i+1] = max(money[i-1] + nums[i], money[i])
18+
19+
return money[-1]
20+
21+
tc = [
22+
([1, 2, 3, 1], 4),
23+
([2, 7, 9, 3, 1], 12),
24+
([1, 2, 0, 5, 10], 12)
25+
]
26+
27+
for i, (nums, e) in enumerate(tc, 1):
28+
sol = Solution()
29+
result = sol.rob(nums)
30+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")

0 commit comments

Comments
 (0)