-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouse_Robber.java
More file actions
43 lines (42 loc) · 1.18 KB
/
House_Robber.java
File metadata and controls
43 lines (42 loc) · 1.18 KB
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
import java.io.*;
import java.lang.*;
import java.util.*;
public class House_Robber {
class Solution {
// DP memoization
public int rob(int[] nums) {
int n = nums.length;
int dp[] = new int[n];
Arrays.fill(dp , -1);
return fun(nums , 0 , dp);
}
public static int fun(int[] nums , int index , int[] dp)
{
if(index >= nums.length)
{
return 0;
}
if(dp[index] != -1)
{
return dp[index];
}
int t = nums[index] + fun(nums , index + 2 , dp);
int nt = fun(nums , index+1 , dp);
return dp[index] = (int)Math.max(t , nt);
}
// Recursion
// public int rob(int[] nums) {
// return fun(nums , 0);
// }
// public static int fun(int[] nums , int index)
// {
// if(index >= nums.length)
// {
// return 0;
// }
// int t = nums[index] + fun(nums , index + 2);
// int nt = fun(nums , index+1);
// return (int)Math.max(t , nt);
// }
}
}