Skip to content

Commit 107bab8

Browse files
committed
add: house-robber
1 parent ee69172 commit 107bab8

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LeetCode
22

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) 
44
![](https://img.shields.io/badge/language-Java-yellow?style=for-the-badge&logo=appveyor.svg)
55

66
LeetCode Solution in Java
@@ -373,6 +373,7 @@ LeetCode Solution in Java
373373
|96|[Unique Binary Search Trees (Classic)](https://leetcode.com/problems/unique-binary-search-trees/)|[Java](tree/unique-binary-search-trees/README.md)|Medium|
374374
|115|[Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/)|[Java](array-string/distinct-subsequences/README.md)|Hard|
375375
|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|
376377
|221|[Maximal Square (Classic)](https://leetcode.com/problems/maximal-square/submissions/)|[Java](dynamic-programming/submissions/README.md)|Medium|
377378
|300|[Longest Increasing Subsequence (Classic)](https://leetcode.com/problems/longest-increasing-subsequence/)|[Java](dynamic-programming/longest-increasing-subsequence/README.md)|Medium|
378379
|313|[Super Ugly Number (Classic)](https://leetcode.com/problems/super-ugly-number/)|[Java](dynamic-programming/super-ugly-number/README.md)|Medium|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
```

0 commit comments

Comments
 (0)