Skip to content

Commit

Permalink
[Leo] 10th Week solutions (3,4 Qs)
Browse files Browse the repository at this point in the history
  • Loading branch information
leokim0922 committed Jul 6, 2024
1 parent 84335f1 commit 0f2057e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions house-robber-ii/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return nums[0]

def robrob(nums):
rob1, rob2 = 0, 0
for i in range(len(nums)):
rob1, rob2 = rob2, max(nums[i] + rob1, rob2)

return rob2

return max(robrob(nums[:n - 1]), robrob(nums[1:]))

## TC: O(n), SC: O(1)
10 changes: 10 additions & 0 deletions house-robber/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def rob(self, nums: List[int]) -> int:
rob, not_rob = 0, 0

for i in nums:
rob, not_rob = not_rob + i, max(rob, not_rob)

return max(rob, not_rob)

## TC: O(n), SC: O(1)

0 comments on commit 0f2057e

Please sign in to comment.