-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC 198. House Robber.java
57 lines (48 loc) · 1.39 KB
/
LC 198. House Robber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Recursion with memoization
class Solution {
public int rob(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp, -1);
return f(nums.length - 1, nums, dp);
}
int f(int ind, int[] nums, int[] dp) {
if (ind == 0) return nums[0];
if (ind < 0) return 0;
if (dp[ind] != -1) return dp[ind];
int pick = nums[ind] + f(ind - 2, nums, dp);
int notPick = f(ind - 1, nums, dp);
return dp[ind] = Math.max(pick, notPick);
}
}
// Tabulation
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0];
int[] dp = new int[n];
dp[n - 1] = nums[n - 1];
dp[n - 2] = Math.max(nums[n - 2], nums[n-1]);
for (int i = n - 3; i >= 0; i--) {
int left = dp[i + 2] + nums[i];
int right = dp[i + 1];
dp[i] = Math.max(left, right);
}
return dp[0];
}
}
// Space optimized
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0];
int next2 = nums[n - 1];
int next = Math.max(nums[n - 2], nums[n-1]);
for (int i = n - 3; i >= 0; i--) {
int left = next2 + nums[i];
int right = next;
next2 = next;
next = Math.max(left, right);
}
return next;
}
}