forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: solve DaleStudy#264 with python
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
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,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() |