Skip to content

Commit d0163ef

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent db5f202 commit d0163ef

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

dynamic_programming/house_robber.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,34 @@
2020
### (3,counter=True) (3,counter=False) (counter=True mean 1 is choosen,so avoid 3)
2121
# / / \
2222
### (2,counter=False) (2,True) (2,False)
23-
# (here 2,False means 1 is choosen, so we avoid 3 and
23+
# (here 2,False means 1 is choosen, so we avoid 3 and
2424
# as 3 is avoided now again we are free to choose at house 2)
2525

2626

2727
## Note: dp[i][0]=>0 is True and 1 is False in convention.
2828
def rob(nums):
29-
def helper(nums,dp,i,j):
30-
if i<0:
29+
def helper(nums, dp, i, j):
30+
if i < 0:
3131
return 0
32-
if dp[i][j]!=-1:
32+
if dp[i][j] != -1:
3333
return dp[i][j]
34-
if j==True:
35-
dp[i][0]=helper(nums,dp,i-1,False)
34+
if j == True:
35+
dp[i][0] = helper(nums, dp, i - 1, False)
3636
return dp[i][0]
3737
else:
38-
dp[i][1] = max(nums[i] + helper(nums,dp,i-1,True), helper(nums,dp,i-1,False))
38+
dp[i][1] = max(
39+
nums[i] + helper(nums, dp, i - 1, True), helper(nums, dp, i - 1, False)
40+
)
3941
return dp[i][1]
40-
dp=[[-1]*2 for _ in range(0,len(nums))]
41-
return helper(nums,dp,len(nums)-1,False)
42-
42+
43+
dp = [[-1] * 2 for _ in range(0, len(nums))]
44+
return helper(nums, dp, len(nums) - 1, False)
45+
46+
4347
if __name__ == "__main__":
44-
nums=[2,7,9,3,1]
45-
if not nums:
46-
print('The list is empty')
48+
nums = [2, 7, 9, 3, 1]
49+
if not nums:
50+
print("The list is empty")
4751
else:
48-
max_amount=rob(nums)
52+
max_amount = rob(nums)
4953
print(max_amount)
50-
51-
52-

0 commit comments

Comments
 (0)