Skip to content

Commit 6236e7f

Browse files
authored
HouseRobber.java file created (dubesar#607)
1 parent 893534d commit 6236e7f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package DP;
2+
3+
// https://leetcode.com/problems/house-robber/
4+
public class HouseRobber {
5+
public int rob(int[] nums) {
6+
if(nums.length == 0) {
7+
return 0;
8+
}
9+
10+
if(nums.length == 1) {
11+
return nums[0];
12+
}
13+
14+
if(nums.length == 2){
15+
Math.max(nums[0],nums[1]);
16+
}
17+
18+
int n = nums.length;
19+
int dp[] = new int[n];
20+
dp[0] = nums[0];
21+
dp[1] = Math.max(nums[0], nums[1]);
22+
23+
for(int i=2; i<n; i++){
24+
dp[i] = Math.max(dp[i-1], (dp[i-2]+nums[i]));
25+
}
26+
27+
return dp[n-1];
28+
}
29+
}

0 commit comments

Comments
 (0)