We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f6bcd2d commit 4a3f2dcCopy full SHA for 4a3f2dc
house-robber/sora0319.java
@@ -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