Skip to content

Commit 65c2c59

Browse files
committed
Solved 198. House Robber using Python code
1 parent f7ae499 commit 65c2c59

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

house-robber/KwonNayeon.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Title: 198. House Robber
3+
4+
Constraints:
5+
- 1 <= nums.length <= 100
6+
- 0 <= nums[i] <= 400
7+
8+
Time Complexity:
9+
- O(n)
10+
Space Complexity:
11+
- O(n)
12+
"""
13+
14+
class Solution:
15+
def rob(self, nums: List[int]) -> int:
16+
17+
if len(nums) == 1:
18+
return nums[0]
19+
elif len(nums) == 2:
20+
return max(nums[0], nums[1])
21+
22+
dp = [0]*len(nums)
23+
dp[0] = nums[0]
24+
dp[1] = max(nums[0], nums[1])
25+
26+
for i in range(2, len(nums)):
27+
dp[i] = max(dp[i-1], nums[i] + dp[i-2])
28+
return dp[-1]

0 commit comments

Comments
 (0)