Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit68a0f6b

Browse files
refactor 213
1 parent2ed7661 commit68a0f6b

File tree

2 files changed

+92
-23
lines changed

2 files changed

+92
-23
lines changed

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

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,59 @@
33
publicclass_213 {
44
publicstaticclassSolution1 {
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
publicintrob(int[]nums) {
12-
if (nums ==null ||nums.length ==0) {
13-
return0;
14-
}
1511
if (nums.length ==1) {
1612
returnnums[0];
1713
}
18-
if (nums.length ==2) {
19-
returnMath.max(nums[0],nums[1]);
20-
}
21-
int[]dp =newint[nums.length -1];
14+
returnMath.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 (inti =2;i <nums.length -1;i++) {
17+
privateintrob(int[]nums,intstart,intend) {
18+
int[]dp =newint[nums.length];
19+
dp[start] =nums[start];
20+
if (end -start <=1) {
21+
returnnums[start];
22+
}
23+
dp[start +1] =Math.max(nums[start],nums[start +1]);
24+
for (inti =start +2;i <end;i++) {
2725
dp[i] =Math.max(dp[i -2] +nums[i],dp[i -1]);
2826
}
29-
intprevMax =dp[nums.length -2];
27+
returndp[end -1];
28+
}
29+
}
3030

31-
//rob 2 to n
32-
dp =newint[nums.length -1];
33-
dp[0] =nums[1];
34-
dp[1] =Math.max(nums[1],nums[2]);
35-
for (inti =3;i <nums.length;i++) {
36-
dp[i -1] =Math.max(dp[i -3] +nums[i],dp[i -2]);
31+
publicstaticclassSolution2 {
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+
publicintrob(int[]nums) {
40+
if (nums.length ==1) {
41+
returnnums[0];
42+
}
43+
returnMath.max(rob(nums,0,nums.length -1),rob(nums,1,nums.length));
44+
}
45+
46+
privateintrob(int[]nums,intstart,intend) {
47+
intfirst =nums[start];
48+
if (end -start <=1) {
49+
returnfirst;
50+
}
51+
intsecond =Math.max(nums[start],nums[start +1]);
52+
inttmp;
53+
for (inti =start +2;i <end;i++) {
54+
tmp =Math.max(second,first +nums[i]);
55+
first =second;
56+
second =tmp;
3757
}
38-
returnMath.max(prevMax,dp[nums.length -2]);
58+
returnMath.max(first,second);
3959
}
4060
}
4161
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._213;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.TestCase.assertEquals;
8+
9+
publicclass_213Test {
10+
privatestatic_213.Solution1solution1;
11+
privatestatic_213.Solution2solution2;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_213.Solution1();
16+
solution2 =new_213.Solution2();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
assertEquals(3,solution1.rob(newint[]{2,3,2}));
22+
assertEquals(3,solution2.rob(newint[]{2,3,2}));
23+
}
24+
25+
@Test
26+
publicvoidtest2() {
27+
assertEquals(4,solution1.rob(newint[]{1,2,3,1}));
28+
assertEquals(4,solution2.rob(newint[]{1,2,3,1}));
29+
}
30+
31+
@Test
32+
publicvoidtest3() {
33+
assertEquals(0,solution1.rob(newint[]{0}));
34+
assertEquals(0,solution2.rob(newint[]{0}));
35+
}
36+
37+
@Test
38+
publicvoidtest4() {
39+
assertEquals(0,solution1.rob(newint[]{0,0}));
40+
assertEquals(0,solution2.rob(newint[]{0,0}));
41+
}
42+
43+
@Test
44+
publicvoidtest5() {
45+
assertEquals(340,solution1.rob(newint[]{200,3,140,20,10}));
46+
assertEquals(340,solution2.rob(newint[]{200,3,140,20,10}));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp