We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a5a0744 commit 5762ba2Copy full SHA for 5762ba2
1 file changed
โhouse-robber/gcount85.pyโ
@@ -0,0 +1,27 @@
1
+"""
2
+# Intuition
3
+DP๋ฅผ ์ฌ์ฉํ๋ค.
4
+
5
+# Approach
6
+ํน์ ์ง์ ์ต๋ money ๊ฐ = max(์ ์ ์ง์ ํธ์์ ๊ฒฝ์ฐ์ ์ต๋๊ฐ + ํ์ฌ ์ง์ money ๊ฐ, ์ด์ ์ง์ ํด ๊ฒฝ์ฐ์ ์ต๋๊ฐ)
7
8
+# Complexity
9
+- Time complexity: O(N)
10
+- Space complexity: O(N)
11
12
13
14
+class Solution:
15
+ def rob(self, nums: List[int]) -> int:
16
+ n = len(nums)
17
+ dp = [0] * n # O(N)
18
19
+ if n == 1:
20
+ return nums[0]
21
22
+ dp[0] = nums[0]
23
+ dp[1] = max(dp[0], nums[1])
24
+ for house in range(2, n): # O(N)
25
+ dp[house] = max(dp[house - 1], dp[house - 2] + nums[house])
26
27
+ return dp[n - 1]
0 commit comments