File tree Expand file tree Collapse file tree 2 files changed +61
-1
lines changed
dynamic-programming/house-robber Expand file tree Collapse file tree 2 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 1
1
# LeetCode
2
2
3
- ![ ] ( https://img.shields.io/badge/solved-226 -337ab7?style=for-the-badge&logo=appveyor.svg )   ;
3
+ ![ ] ( https://img.shields.io/badge/solved-227 -337ab7?style=for-the-badge&logo=appveyor.svg )   ;
4
4
![ ] ( https://img.shields.io/badge/language-Java-yellow?style=for-the-badge&logo=appveyor.svg )
5
5
6
6
LeetCode Solution in Java
@@ -373,6 +373,7 @@ LeetCode Solution in Java
373
373
| 96| [ Unique Binary Search Trees (Classic)] ( https://leetcode.com/problems/unique-binary-search-trees/ ) | [ Java] ( tree/unique-binary-search-trees/README.md ) | Medium|
374
374
| 115| [ Distinct Subsequences] ( https://leetcode.com/problems/distinct-subsequences/ ) | [ Java] ( array-string/distinct-subsequences/README.md ) | Hard|
375
375
| 131| [ Palindrome Partitioning] ( https://leetcode.com/problems/palindrome-partitioning/ ) | [ Java] ( dynamic-programming/palindrome-partitioning/README.md ) | Medium|
376
+ | 198| [ House Robber] ( https://leetcode.com/problems/house-robber/ ) | [ Java] ( dynamic-programming/house-robber/README.md ) | Medium|
376
377
| 221| [ Maximal Square (Classic)] ( https://leetcode.com/problems/maximal-square/submissions/ ) | [ Java] ( dynamic-programming/submissions/README.md ) | Medium|
377
378
| 300| [ Longest Increasing Subsequence (Classic)] ( https://leetcode.com/problems/longest-increasing-subsequence/ ) | [ Java] ( dynamic-programming/longest-increasing-subsequence/README.md ) | Medium|
378
379
| 313| [ Super Ugly Number (Classic)] ( https://leetcode.com/problems/super-ugly-number/ ) | [ Java] ( dynamic-programming/super-ugly-number/README.md ) | Medium|
Original file line number Diff line number Diff line change
1
+ # House Robber
2
+
3
+ ## Solution 1
4
+
5
+ DFS
6
+
7
+ ``` java
8
+ /**
9
+ * Question : 198. House Robber
10
+ * Complexity : Time: O(2^n) ; Space: O(n)
11
+ * Topics : DP
12
+ */
13
+ class Solution {
14
+ public int rob (int [] nums ) {
15
+ if (nums == null || nums. length == 0 ) {
16
+ return 0 ;
17
+ }
18
+ return robUtil(nums, nums. length);
19
+ }
20
+
21
+ private int robUtil (int [] nums , int n ) {
22
+ if (n <= 0 ) {
23
+ return 0 ;
24
+ }
25
+ return Math . max(nums[n - 1 ] + robUtil(nums, n - 2 ), robUtil(nums, n - 1 ));
26
+ }
27
+ }
28
+ ```
29
+
30
+ ## Solution 2
31
+
32
+ DP
33
+
34
+ ``` java
35
+ /**
36
+ * Question : 198. House Robber
37
+ * Complexity : Time: O(n) ; Space: O(n)
38
+ * Topics : DP
39
+ */
40
+ class Solution {
41
+ public int rob (int [] nums ) {
42
+ if (nums == null || nums. length == 0 ) {
43
+ return 0 ;
44
+ }
45
+
46
+ int n = nums. length;
47
+
48
+ int [] dp = new int [n + 1 ];
49
+ dp[0 ] = 0 ;
50
+ dp[1 ] = nums[0 ];
51
+
52
+ for (int i = 2 ; i <= n; i++ ) {
53
+ dp[i] = Math . max(dp[i - 1 ], nums[i - 1 ] + dp[i - 2 ]);
54
+ }
55
+
56
+ return dp[n];
57
+ }
58
+ }
59
+ ```
You can’t perform that action at this time.
0 commit comments