Skip to content

Commit 4a3f2dc

Browse files
committed
house rober solved
1 parent f6bcd2d commit 4a3f2dc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

house-robber/sora0319.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.*;
2+
class Solution {
3+
public int rob(int[] nums) {
4+
int[] house = new int[nums.length];
5+
Arrays.fill(house, -1);
6+
return maxRobbery(0, nums, house);
7+
}
8+
9+
private int maxRobbery(int index, int[] nums, int[] house) {
10+
if (index >= nums.length) return 0;
11+
if (house[index] != -1) return house[index];
12+
13+
int rob = nums[index] + maxRobbery(index + 2, nums, house);
14+
int skip = maxRobbery(index + 1, nums, house);
15+
16+
house[index] = Math.max(rob, skip);
17+
return house[index];
18+
}
19+
}

0 commit comments

Comments
 (0)