-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouseRobber.py
More file actions
21 lines (18 loc) · 784 Bytes
/
HouseRobber.py
File metadata and controls
21 lines (18 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Question link - https://leetcode.com/problems/house-robber/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def rob(self, nums: List[int]) -> int:
# In this approch , I am using the two rob house
# Which conatins maximum of the money value
# Rob1 , rob2
# We can only include rob1 + n , but not inluce rob2
# When we include rob2 , we can't inluce rob1 and n
rob1 , rob2 = 0,0
# Traverse the house in street
# [rob1 , rob2 , n , n+1 , n+2 , ....]
for n in nums:
# Temp -> maximum last rob money from rob1 and rob2
temp = max(rob1 + n , rob2)
rob1 = rob2
rob2 = temp
# Return the value maximum for the rob houses
return rob2