From 7686937959c122967d85eafe78d52ef86108bf34 Mon Sep 17 00:00:00 2001 From: Amitabh Kumar <68918540+kamitabh24@users.noreply.github.com> Date: Sat, 21 Oct 2023 21:07:34 +0530 Subject: [PATCH] Create Jump Game --- Pull Here/LeetCode/Jump Game | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Pull Here/LeetCode/Jump Game diff --git a/Pull Here/LeetCode/Jump Game b/Pull Here/LeetCode/Jump Game new file mode 100644 index 0000000..c92e8dd --- /dev/null +++ b/Pull Here/LeetCode/Jump Game @@ -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; + } + } +}