Skip to content

Commit b32e4cf

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 198_House_Robber.java
1 parent 1f6f5d8 commit b32e4cf

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int[] memo = new int[nums.length + 1];
8+
Arrays.fill(memo, -1);
9+
return robHouses(nums, nums.length - 1, memo);
10+
}
11+
12+
private int robHouses(int[] houses, int houseNo, int[] memo) {
13+
if (houseNo < 0) {
14+
return 0;
15+
}
16+
if (memo[houseNo] >= 0) {
17+
return memo[houseNo];
18+
}
19+
20+
int result = Math.max(robHouses(houses, houseNo - 2, memo) + houses[houseNo],
21+
robHouses(houses, houseNo - 1, memo));
22+
memo[houseNo] = result;
23+
return result;
24+
}
25+
}

0 commit comments

Comments
 (0)