File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+ class Solution :
4+ """
5+ - Time Complexity: O(n), n = len(nums)
6+ - Space Complexity: O(n), n = len(nums)
7+ """
8+ def rob (self , nums : List [int ]) -> int :
9+ # DP Formation
10+ # money[0] = 0
11+ # money[1] = nums[0]
12+ # money[i+1] = max(money[i-1] + nums[i], money[i])
13+
14+ money = [0 ] * (len (nums ) + 1 )
15+ money [1 ] = nums [0 ]
16+ for i in range (1 , len (nums )):
17+ money [i + 1 ] = max (money [i - 1 ] + nums [i ], money [i ])
18+
19+ return money [- 1 ]
20+
21+ tc = [
22+ ([1 , 2 , 3 , 1 ], 4 ),
23+ ([2 , 7 , 9 , 3 , 1 ], 12 ),
24+ ([1 , 2 , 0 , 5 , 10 ], 12 )
25+ ]
26+
27+ for i , (nums , e ) in enumerate (tc , 1 ):
28+ sol = Solution ()
29+ result = sol .rob (nums )
30+ print (f"TC { i } is Passed!" if result == e else f"TC { i } is Failed! - Expected: { e } , Result: { result } " )
You can’t perform that action at this time.
0 commit comments