Skip to content

Commit 68a0f6b

Browse files
refactor 213
1 parent 2ed7661 commit 68a0f6b

File tree

2 files changed

+92
-23
lines changed

2 files changed

+92
-23
lines changed

src/main/java/com/fishercoder/solutions/_213.java

+43-23
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,59 @@
33
public class _213 {
44
public static class Solution1 {
55
/**
6-
* Another dp problem:
7-
* separate them into two cases:
8-
* 1. rob from house 1 to n-1, get max1
9-
* 2. rob from house 2 to n, get max2 take the max from the above two max
6+
* Basically there are two ways to rob the houses: one is to rob from 0 to n - 1, the other is to rob from 1 to n, and then take the max from these two.
7+
* Time: O(n)
8+
* Space: O(n)
109
*/
1110
public int rob(int[] nums) {
12-
if (nums == null || nums.length == 0) {
13-
return 0;
14-
}
1511
if (nums.length == 1) {
1612
return nums[0];
1713
}
18-
if (nums.length == 2) {
19-
return Math.max(nums[0], nums[1]);
20-
}
21-
int[] dp = new int[nums.length - 1];
14+
return Math.max(rob(nums, 0, nums.length - 1), rob(nums, 1, nums.length));
15+
}
2216

23-
//rob 1 to n-1
24-
dp[0] = nums[0];
25-
dp[1] = Math.max(nums[0], nums[1]);
26-
for (int i = 2; i < nums.length - 1; i++) {
17+
private int rob(int[] nums, int start, int end) {
18+
int[] dp = new int[nums.length];
19+
dp[start] = nums[start];
20+
if (end - start <= 1) {
21+
return nums[start];
22+
}
23+
dp[start + 1] = Math.max(nums[start], nums[start + 1]);
24+
for (int i = start + 2; i < end; i++) {
2725
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
2826
}
29-
int prevMax = dp[nums.length - 2];
27+
return dp[end - 1];
28+
}
29+
}
3030

31-
//rob 2 to n
32-
dp = new int[nums.length - 1];
33-
dp[0] = nums[1];
34-
dp[1] = Math.max(nums[1], nums[2]);
35-
for (int i = 3; i < nums.length; i++) {
36-
dp[i - 1] = Math.max(dp[i - 3] + nums[i], dp[i - 2]);
31+
public static class Solution2 {
32+
/**
33+
* This solution is identical to the above solution 1, the only difference is that instead of using an extra array, we use only two extra variables here to reduce the space complexity to O(1).
34+
* Time: O(n)
35+
* Space: O(1)
36+
* <p>
37+
* Just draw out how this rotation works on a piece of paper, it'll be easy to figure out how first, second and tmp variables keep shifting towards the right of the array.
38+
*/
39+
public int rob(int[] nums) {
40+
if (nums.length == 1) {
41+
return nums[0];
42+
}
43+
return Math.max(rob(nums, 0, nums.length - 1), rob(nums, 1, nums.length));
44+
}
45+
46+
private int rob(int[] nums, int start, int end) {
47+
int first = nums[start];
48+
if (end - start <= 1) {
49+
return first;
50+
}
51+
int second = Math.max(nums[start], nums[start + 1]);
52+
int tmp;
53+
for (int i = start + 2; i < end; i++) {
54+
tmp = Math.max(second, first + nums[i]);
55+
first = second;
56+
second = tmp;
3757
}
38-
return Math.max(prevMax, dp[nums.length - 2]);
58+
return Math.max(first, second);
3959
}
4060
}
4161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._213;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _213Test {
10+
private static _213.Solution1 solution1;
11+
private static _213.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _213.Solution1();
16+
solution2 = new _213.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(3, solution1.rob(new int[]{2, 3, 2}));
22+
assertEquals(3, solution2.rob(new int[]{2, 3, 2}));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(4, solution1.rob(new int[]{1, 2, 3, 1}));
28+
assertEquals(4, solution2.rob(new int[]{1, 2, 3, 1}));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(0, solution1.rob(new int[]{0}));
34+
assertEquals(0, solution2.rob(new int[]{0}));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
assertEquals(0, solution1.rob(new int[]{0, 0}));
40+
assertEquals(0, solution2.rob(new int[]{0, 0}));
41+
}
42+
43+
@Test
44+
public void test5() {
45+
assertEquals(340, solution1.rob(new int[]{200, 3, 140, 20, 10}));
46+
assertEquals(340, solution2.rob(new int[]{200, 3, 140, 20, 10}));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)