-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fa9aa5
commit 92316a9
Showing
10 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
algorithms/dynamic_programming/house_robber/test_house_robber.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
File renamed without changes.
File renamed without changes.
File renamed without changes.