Skip to content

Commit

Permalink
feat(dp): house robber
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Oct 24, 2023
1 parent 0fa9aa5 commit 92316a9
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 0 deletions.
File renamed without changes.
28 changes: 28 additions & 0 deletions algorithms/dynamic_programming/house_robber/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed,
the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and
it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can
rob tonight without alerting the police.

```plain
Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
```

## Related Topics

- Array
- Dynamic Programming
12 changes: 12 additions & 0 deletions algorithms/dynamic_programming/house_robber/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List


def rob(nums: List[int]) -> int:
"""Uses a top-down approach using Rolling Window technique where the idea is to only remember what is the maximum
gain at the next three houses from the current position."""
current, previous = 0, 0

for house in nums:
current, previous = max(previous + house, current), current

return current
43 changes: 43 additions & 0 deletions algorithms/dynamic_programming/house_robber/test_house_robber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest
from . import rob


class MyTestCase(unittest.TestCase):
def test_1(self):
"""should return 4 for nums = [1,2,3,1]"""
nums = [1, 2, 3, 1]
expected = 4
actual = rob(nums)
self.assertEqual(expected, actual)

def test_2(self):
"""should return 4 for nums = [2,7,9,3,1]"""
nums = [2, 7, 9, 3, 1]
expected = 12
actual = rob(nums)
self.assertEqual(expected, actual)

def test_3(self):
"""should return 0 for nums = []"""
nums = []
expected = 0
actual = rob(nums)
self.assertEqual(expected, actual)

def test_4(self):
"""should return 3 for nums = [3]"""
nums = [3]
expected = 3
actual = rob(nums)
self.assertEqual(expected, actual)

def test_5(self):
"""should return 3 for nums = [3, 5]"""
nums = [3, 5]
expected = 5
actual = rob(nums)
self.assertEqual(expected, actual)


if __name__ == '__main__':
unittest.main()

0 comments on commit 92316a9

Please sign in to comment.