From d5657ab05bffe10431bb26a61ef2362422cd241b Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 16 Apr 2026 04:32:46 -0600 Subject: [PATCH] adding uptes --- .../ex_133_house_robber.py | 29 +++++++++++++ .../ex_133_house_robber.ts | 29 +++++++++++++ .../test_133_house_robber_round_22.py | 42 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_133_house_robber.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_133_house_robber.ts create mode 100644 tests/test_150_questions_round_22/test_133_house_robber_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_133_house_robber.py b/src/my_project/interviews/top_150_questions_round_22/ex_133_house_robber.py new file mode 100644 index 00000000..4062a168 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_133_house_robber.py @@ -0,0 +1,29 @@ +from typing import List + + +class Solution: + def rob(self, nums: List[int]) -> int: + """ + Dynamic Programming approach: + - For each house, we choose max of: + 1. Rob current house + max money from i-2 houses + 2. Skip current house and take max money from i-1 houses + + Time: O(n), Space: O(1) + """ + if not nums: + return 0 + if len(nums) == 1: + return nums[0] + + # prev2: max money robbed up to i-2 + # prev1: max money robbed up to i-1 + prev2 = nums[0] + prev1 = max(nums[0], nums[1]) + + for i in range(2, len(nums)): + current = max(nums[i] + prev2, prev1) + prev2 = prev1 + prev1 = current + + return prev1 \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_133_house_robber.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_133_house_robber.ts new file mode 100644 index 00000000..698fefe6 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_133_house_robber.ts @@ -0,0 +1,29 @@ +function rob(nums: number[]): number { + /** + * Dynamic Programming approach: + * - For each house, we choose max of: + * 1. Rob current house + max money from i-2 houses + * 2. Skip current house and take max money from i-1 houses + * + * Time: O(n), Space: O(1) + */ + if (nums.length === 0) return 0; + if (nums.length === 1) return nums[0]; + + // prev2: max money robbed up to i-2 + // prev1: max money robbed up to i-1 + let prev2 = nums[0]; + let prev1 = Math.max(nums[0], nums[1]); + + for (let i = 2; i < nums.length; i++) { + const current = Math.max(nums[i] + prev2, prev1); + prev2 = prev1; + prev1 = current; + } + + return prev1; +} + +// Example usage: +// console.log(rob([1, 2, 3, 1])); // Output: 4 +// console.log(rob([2, 7, 9, 3, 1])); // Output: 12 \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_133_house_robber_round_22.py b/tests/test_150_questions_round_22/test_133_house_robber_round_22.py new file mode 100644 index 00000000..20093987 --- /dev/null +++ b/tests/test_150_questions_round_22/test_133_house_robber_round_22.py @@ -0,0 +1,42 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_133_house_robber import Solution + + +class HouseRobberTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + """Test case: [1,2,3,1] -> 4""" + nums = [1, 2, 3, 1] + self.assertEqual(self.solution.rob(nums), 4) + + def test_example_2(self): + """Test case: [2,7,9,3,1] -> 12""" + nums = [2, 7, 9, 3, 1] + self.assertEqual(self.solution.rob(nums), 12) + + def test_single_house(self): + """Test case: Single house""" + nums = [5] + self.assertEqual(self.solution.rob(nums), 5) + + def test_two_houses(self): + """Test case: Two houses""" + nums = [1, 2] + self.assertEqual(self.solution.rob(nums), 2) + + def test_all_same_value(self): + """Test case: All houses have same value""" + nums = [5, 5, 5, 5, 5] + self.assertEqual(self.solution.rob(nums), 15) + + def test_ascending_values(self): + """Test case: Ascending values""" + nums = [1, 2, 3, 4, 5] + self.assertEqual(self.solution.rob(nums), 9) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file