Skip to content

Commit 1a41e75

Browse files
refactor 213
1 parent fe10936 commit 1a41e75

File tree

1 file changed

+38
-35
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+38
-35
lines changed
Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
11
package com.fishercoder.solutions;
2-
/**213. House Robber II
2+
/**
3+
* 213. House Robber II
34
45
Note: This is an extension of House Robber.
56
6-
After robbing those houses on that street,
7+
After robbing those houses on that street,
78
the thief has found himself a new place for his thievery
89
so that he will not get too much attention.
910
This time, all houses at this place are arranged in a circle.
1011
That means the first house is the neighbor of the last one.
1112
Meanwhile, the security system for these houses remain the same as for those in the previous street.
1213
13-
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.
14+
Given a list of non-negative integers representing the amount of money of each house,
15+
determine the maximum amount of money you can rob tonight without alerting the police.
1416
*/
1517
public class _213 {
18+
public static class Solution1 {
19+
/**
20+
* Another dp problem:
21+
* separate them into two cases:
22+
* 1. rob from house 1 to n-1, get max1
23+
* 2. rob from house 2 to n, get max2 take the max from the above two max
24+
*/
25+
public int rob(int[] nums) {
26+
if (nums == null || nums.length == 0) {
27+
return 0;
28+
}
29+
if (nums.length == 1) {
30+
return nums[0];
31+
}
32+
if (nums.length == 2) {
33+
return Math.max(nums[0], nums[1]);
34+
}
35+
int[] dp = new int[nums.length - 1];
1636

17-
/**Another dp problem:
18-
* separate them into two cases:
19-
* 1. rob from house 1 to n-1, get max1
20-
* 2. rob from house 2 to n, get max2
21-
* take the max from the above two max*/
22-
public int rob(int[] nums) {
23-
if (nums == null || nums.length == 0) {
24-
return 0;
25-
}
26-
if (nums.length == 1) {
27-
return nums[0];
28-
}
29-
if (nums.length == 2) {
30-
return Math.max(nums[0], nums[1]);
31-
}
32-
int[] dp = new int[nums.length - 1];
37+
//rob 1 to n-1
38+
dp[0] = nums[0];
39+
dp[1] = Math.max(nums[0], nums[1]);
40+
for (int i = 2; i < nums.length - 1; i++) {
41+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
42+
}
43+
int prevMax = dp[nums.length - 2];
3344

34-
//rob 1 to n-1
35-
dp[0] = nums[0];
36-
dp[1] = Math.max(nums[0], nums[1]);
37-
for (int i = 2; i < nums.length - 1; i++) {
38-
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
45+
//rob 2 to n
46+
dp = new int[nums.length - 1];
47+
dp[0] = nums[1];
48+
dp[1] = Math.max(nums[1], nums[2]);
49+
for (int i = 3; i < nums.length; i++) {
50+
dp[i - 1] = Math.max(dp[i - 3] + nums[i], dp[i - 2]);
51+
}
52+
return Math.max(prevMax, dp[nums.length - 2]);
3953
}
40-
int prevMax = dp[nums.length - 2];
41-
42-
//rob 2 to n
43-
dp = new int[nums.length - 1];
44-
dp[0] = nums[1];
45-
dp[1] = Math.max(nums[1], nums[2]);
46-
for (int i = 3; i < nums.length; i++) {
47-
dp[i - 1] = Math.max(dp[i - 3] + nums[i], dp[i - 2]);
48-
}
49-
return Math.max(prevMax, dp[nums.length - 2]);
5054
}
51-
5255
}

0 commit comments

Comments
 (0)