From 7643d32e0b7614c75d01f6847487642fdf49936b Mon Sep 17 00:00:00 2001 From: EGON Date: Sun, 10 Nov 2024 03:59:59 +0900 Subject: [PATCH] feat: solve #264 with python --- house-robber/EGON.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 house-robber/EGON.py diff --git a/house-robber/EGON.py b/house-robber/EGON.py new file mode 100644 index 000000000..e6fc8db52 --- /dev/null +++ b/house-robber/EGON.py @@ -0,0 +1,42 @@ +from typing import List +from unittest import TestCase, main + + +class Solution: + def rob(self, nums: List[int]) -> int: + return self.solve_dp(nums) + + """ + Runtime: 0 ms (Beats 100.00%) + Time Complexity: O(n) + - nums 배열을 조회하며 dp 배열을 갱신하므로 O(n) + - 2항에 대한 max 연산을 사용하므로 * O(2) + > O(2 * n) ~= O(n) + + Memory: 16.62 MB (Beats 24.05%) + Space Complexity: O(n) + > 길이가 n인 dp 배열을 사용하므로 O(n) + """ + + def solve_dp(self, nums: List[int]) -> int: + if len(nums) <= 2: + return max(nums) + + dp = [0] * len(nums) + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + for i in range(2, len(nums)): + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) + + return dp[-1] + + +class _LeetCodeTestCases(TestCase): + def test_1(self): + nums = [2,1,1,2] + output = 4 + self.assertEqual(Solution().rob(nums), output) + + +if __name__ == '__main__': + main()