Skip to content

Commit 0f2057e

Browse files
committed
[Leo] 10th Week solutions (3,4 Qs)
1 parent 84335f1 commit 0f2057e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

house-robber-ii/Leo.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
if n == 1:
5+
return nums[0]
6+
7+
def robrob(nums):
8+
rob1, rob2 = 0, 0
9+
for i in range(len(nums)):
10+
rob1, rob2 = rob2, max(nums[i] + rob1, rob2)
11+
12+
return rob2
13+
14+
return max(robrob(nums[:n - 1]), robrob(nums[1:]))
15+
16+
## TC: O(n), SC: O(1)

house-robber/Leo.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
rob, not_rob = 0, 0
4+
5+
for i in nums:
6+
rob, not_rob = not_rob + i, max(rob, not_rob)
7+
8+
return max(rob, not_rob)
9+
10+
## TC: O(n), SC: O(1)

0 commit comments

Comments
 (0)