-
Notifications
You must be signed in to change notification settings - Fork 5
/
HouseRobberII.java
43 lines (36 loc) · 1.65 KB
/
HouseRobberII.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
/*
Problem Statement:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Problem Link:
House Robber II:https://leetcode.com/problems/house-robber-ii/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/HouseRobberII.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
public int rob(int[] nums) {
if(nums.length==0)
return 0;
if(nums.length==1)
return nums[0];
int dp1[][] = new int[nums.length+1][2];
for(int i =1;i<nums.length;i++){
dp1[i][0] = Math.max(dp1[i-1][0], dp1[i-1][1]);
dp1[i][1] = dp1[i-1][0] + nums[i-1];
}
int dp2[][] = new int[nums.length+1][2];
for(int i =2;i<=nums.length;i++){
dp2[i][0] = Math.max(dp2[i-1][0], dp2[i-1][1]);
dp2[i][1] = dp2[i-1][0] + nums[i-1];
}
return Math.max(
Math.max(dp1[nums.length-1][0], dp1[nums.length-1][1]),
Math.max(dp2[nums.length][0], dp2[nums.length][1])
);
}
}