Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Jump Game #142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Pull Here/LeetCode/Jump Game
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
public class Solution {

public int jump(int[] A) {
int jumps = 0, curEnd = 0, curFarthest = 0;
for (int i = 0; i < A.length - 1; i++) { // @note: i < Len - 1. 也就是停在倒数第二个
curFarthest = Math.max(curFarthest, i + A[i]);
if (i == curEnd) {
jumps++;
curEnd = curFarthest;

if (curEnd >= A.length - 1) {
break;
}
}
}
return jumps;
}
}



public class Jump_Game_II {

class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}

int left = 0; // last jump reaching to
int right = 0; // current jump reaching to

int count = 0;

for (int i = 0; i < nums.length - 1; i++) { // @note: i < Len - 1. 也就是停在倒数第二个
right = Math.max(right, i + nums[i]);
if (i == left) { // reaching furthest of last jump
count++;
left = right;

if (curEnd >= A.length - 1) {
break;
}
}
}

return count;
}
}
}