Skip to content

Commit 230a1fe

Browse files
committed
leetcode.com 198. House Robber
문제 링크: https://leetcode.com/problems/house-robber
1 parent 5bd16bd commit 230a1fe

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def rob(self, nums: List[int]) -> int:
6+
nums = [0, 0, 0] + nums
7+
dp = [0 for _ in range(len(nums))]
8+
9+
for idx in range(3, len(nums)):
10+
mx = max(dp[idx - 3], dp[idx - 2])
11+
dp[idx] = mx + nums[idx]
12+
13+
return max(dp)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
5+
class TestSolution(TestCase):
6+
def test1_rob(self):
7+
sln = Solution()
8+
self.assertEqual(
9+
4, sln.rob([1,2,3,1])
10+
)
11+
12+
def test2_rob(self):
13+
sln = Solution()
14+
self.assertEqual(
15+
12, sln.rob([2,7,9,3,1])
16+
)

0 commit comments

Comments
 (0)