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

Commit437ff63

Browse files
add a solution for 55
1 parent23e0ca9 commit437ff63

File tree

2 files changed

+136
-7
lines changed

2 files changed

+136
-7
lines changed

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

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,80 @@
33
publicclass_55 {
44

55
publicstaticclassSolution1 {
6+
/**
7+
* My very original but lengthy solution.
8+
*/
69
publicbooleancanJump(int[]nums) {
7-
intfarthest =nums[0];
8-
for (inti =0;i <nums.length;i++) {
9-
if (i <=farthest &&nums[i] +i >farthest) {
10-
//i <= farthest is to make sure that this current i is within the current range
11-
// nums[i]+i > farthest is to make sure that it's necessary to update farthest with current nums[i]+i
12-
farthest =nums[i] +i;
10+
intfurthestReach =nums[0];
11+
if (furthestReach >=nums.length -1) {
12+
returntrue;
13+
}
14+
inti =1;
15+
for (;i <nums.length; ) {
16+
intnewFurthestReach = -1;
17+
while (i <=furthestReach) {
18+
newFurthestReach =Math.max(newFurthestReach,nums[i] +i);
19+
if (newFurthestReach >=nums.length) {
20+
returntrue;
21+
}
22+
i++;
23+
}
24+
if (newFurthestReach <=furthestReach) {
25+
returnfalse;
26+
}elseif (newFurthestReach >=nums.length -1) {
27+
returntrue;
28+
}else {
29+
furthestReach =newFurthestReach;
30+
}
31+
}
32+
returnfalse;
33+
}
34+
}
35+
36+
publicstaticclassSolution2 {
37+
/**
38+
* The same idea as mine above, but much more concise.
39+
* Credit: https://leetcode.com/problems/jump-game/discuss/20917/Linear-and-simple-solution-in-C%2B%2B
40+
*/
41+
publicbooleancanJump(int[]nums) {
42+
inti =0;
43+
for (intreach =0;i <nums.length &&i <=reach;i++) {
44+
reach =Math.max(reach,nums[i] +i);
45+
}
46+
returni >=nums.length;
47+
}
48+
}
49+
50+
publicstaticclassSolution3 {
51+
/**
52+
* Top-down DP.
53+
* Credit: https://leetcode.com/problems/jump-game/solution/ approach 2
54+
* <p>
55+
* Specifically, for this problem, my very own Solution1 and the above Solution2 run much faster than this DP solution.
56+
* But just use this problem to practice DP.
57+
* <p>
58+
* The reason it's called top-down is that it's filling the dp array from the right to the left if you set break points and step through this.
59+
*/
60+
publicbooleancanJump(int[]nums) {
61+
int[]dp =newint[nums.length];
62+
//0 means unknown, 1 means reachable, 2 means unreachable
63+
dp[nums.length -1] =1;
64+
returncanJumpFrom(0,nums,dp);
65+
}
66+
67+
privatebooleancanJumpFrom(intindex,int[]nums,int[]dp) {
68+
if (dp[index] !=0) {
69+
returndp[index] ==1;
70+
}
71+
intfurthestReach =Math.min(index +nums[index],nums.length -1);
72+
for (inti =index +1;i <=furthestReach;i++) {
73+
if (canJumpFrom(i,nums,dp)) {
74+
dp[i] =1;
75+
returntrue;
1376
}
1477
}
15-
returnfarthest >=nums.length -1;
78+
dp[index] =2;
79+
returnfalse;
1680
}
1781
}
1882
}

‎src/test/java/com/fishercoder/_55Test.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,88 @@
88

99
publicclass_55Test {
1010
privatestatic_55.Solution1solution1;
11+
privatestatic_55.Solution2solution2;
12+
privatestatic_55.Solution3solution3;
1113
privatestaticint[]nums;
1214

1315
@BeforeClass
1416
publicstaticvoidsetup() {
1517
solution1 =new_55.Solution1();
18+
solution2 =new_55.Solution2();
19+
solution3 =new_55.Solution3();
1620
}
1721

1822
@Test
1923
publicvoidtest1() {
2024
nums =newint[]{0,2,3};
2125
assertEquals(false,solution1.canJump(nums));
26+
assertEquals(false,solution2.canJump(nums));
27+
assertEquals(false,solution3.canJump(nums));
2228
}
2329

2430
@Test
2531
publicvoidtest2() {
2632
nums =newint[]{1,2};
2733
assertEquals(true,solution1.canJump(nums));
34+
assertEquals(true,solution2.canJump(nums));
35+
assertEquals(true,solution3.canJump(nums));
2836
}
2937

38+
@Test
39+
publicvoidtest3() {
40+
nums =newint[]{2,3,1,1,4};
41+
assertEquals(true,solution1.canJump(nums));
42+
assertEquals(true,solution2.canJump(nums));
43+
assertEquals(true,solution3.canJump(nums));
44+
}
45+
46+
@Test
47+
publicvoidtest4() {
48+
nums =newint[]{5,9,3,2,1,0,2,3,3,1,0,0};
49+
assertEquals(true,solution1.canJump(nums));
50+
assertEquals(true,solution2.canJump(nums));
51+
assertEquals(true,solution3.canJump(nums));
52+
}
53+
54+
@Test
55+
publicvoidtest5() {
56+
nums =newint[]{2,0,0};
57+
assertEquals(true,solution1.canJump(nums));
58+
assertEquals(true,solution2.canJump(nums));
59+
assertEquals(true,solution3.canJump(nums));
60+
}
61+
62+
@Test
63+
publicvoidtest6() {
64+
nums =newint[]{2,0};
65+
assertEquals(true,solution1.canJump(nums));
66+
assertEquals(true,solution2.canJump(nums));
67+
assertEquals(true,solution3.canJump(nums));
68+
}
69+
70+
@Test
71+
publicvoidtest7() {
72+
nums =newint[]{1,1,0,1};
73+
assertEquals(false,solution1.canJump(nums));
74+
assertEquals(false,solution2.canJump(nums));
75+
assertEquals(false,solution3.canJump(nums));
76+
}
77+
78+
@Test
79+
publicvoidtest8() {
80+
nums =newint[]{0};
81+
assertEquals(true,solution1.canJump(nums));
82+
assertEquals(true,solution2.canJump(nums));
83+
assertEquals(true,solution3.canJump(nums));
84+
}
85+
86+
@Test
87+
publicvoidtest9() {
88+
nums =newint[]{3,2,1,0,4};
89+
// assertEquals(false, solution1.canJump(nums));
90+
// assertEquals(false, solution2.canJump(nums));
91+
assertEquals(false,solution3.canJump(nums));
92+
}
93+
94+
3095
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp