Skip to content

Commit 1c9ecfd

Browse files
committed
houser robber
1 parent c2c53ac commit 1c9ecfd

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

house-robber/Sung-Heon.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
if len(nums) == 1:
4+
return nums[0]
5+
if len(nums) == 2:
6+
return max(nums)
7+
8+
dp = [0] * len(nums)
9+
dp[0] = nums[0]
10+
dp[1] = max(nums[0], nums[1])
11+
12+
for i in range(2, len(nums)):
13+
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
14+
15+
return dp[-1]

0 commit comments

Comments
 (0)