File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ μ΄μμ§μ λͺ»ν
, μ£Όμ΄μ§ nums list μ§μ μνμΌλ‘ μ΄μ΄μ Έ μμ (맨 μ²μ <- > 맨 λ§μ§λ§ μ΄μ)
3
+ κ°μ₯ λ§μ λμ κ°μ§κ³ μλ μ§μ μ΅λνμΌλ‘ ν΄ μ΄ κΈμ‘μ λ°ν
4
+
5
+ 1. 첫 μ§μ ν¬ν¨νκ³ , λ§μ§λ§ μ§μ μ μΈνλ κ²½μ° nums[:-1]
6
+ 2. 첫 μ§μ μ μΈνκ³ , λ§μ§λ§ μ§μ ν¬ν¨νλ κ²½μ° nums[1:]
7
+
8
+ TC: O(N),
9
+ SC: O(N)
10
+ """
11
+
12
+ from typing import List
13
+
14
+ class Solution :
15
+ def rob (self , nums : List [int ]) -> int :
16
+ if len (nums ) == 0 :
17
+ return 0
18
+ if len (nums ) == 1 :
19
+ return nums [0 ]
20
+ if len (nums ) == 2 :
21
+ return max (nums )
22
+
23
+ def rob_linear (houses : List [int ]) -> int :
24
+ if len (houses ) == 1 :
25
+ return houses [0 ]
26
+
27
+ dp = [0 ] * len (houses )
28
+ dp [0 ] = houses [0 ]
29
+ dp [1 ] = max (houses [0 ], houses [1 ])
30
+ for i in range (2 , len (houses )):
31
+ dp [i ] = max (dp [i - 1 ], houses [i ] + dp [i - 2 ])
32
+ return dp [- 1 ]
33
+
34
+ case1 = rob_linear (nums [:- 1 ])
35
+ case2 = rob_linear (nums [1 :])
36
+
37
+ return max (case1 , case2 )
You canβt perform that action at this time.
0 commit comments