Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Here is an intuitive explanation of the jump algorithm, broken down into steps. This explanation aims to be memorable so that you can recall the logic at a glance:

**Algorithm: Jump Game (Finding Minimum Jumps to Reach the End)**

Imagine you're on a track with numbered tiles lined up in a row. Each number tells you the maximum number of tiles you can leap forward from that tile. Your goal is to reach the end of the track in as few jumps as possible.

Here's the strategy in memorable steps:

1. **Starting Block:** 
   - You're standing on the first tile, ready to start jumping.

2. **Look Ahead:**
   - Before you jump, you check how far you could potentially leap from your current position and all the positions up to where you'd land. This is your scanning phase.

3. **Plan Your Jump:**
   - Now, you decide where to land by picking the tile that gives you the furthest reach on your next jump. That doesn't mean you jump there directly. It just means you know it's the best tile to aim for.

4. **Leap of Faith:**
   - You make the jump, but only to the next tile. You're not actually leaping all the way to the furthest tile you spotted. It's a controlled, one-tile hop.

5. **Counting Hops:**
   - Every time you reach the furthest tile you previously noted, you count a hop. This is because you've committed to a sequence that you predicted would carry you forward optimally.

6. **Repeat:**
   - You keep doing this—scanning, planning, hopping, and counting—until you're about to land on or pass the final tile.

7. **Finish Line:**
   - When you've made the jump that takes you to or beyond the last tile, you've finished the game, and the number of hops you've counted is the minimum needed to get there.

**Key Points to Remember:**

- **Scan and Plan:** Always look ahead and plan your jumps based on potential future leaps, not just the immediate next step.
- **Incremental Hops:** You move forward one tile at a time, but you're always thinking several tiles ahead.
- **Count Wisely:** You only count a hop when you've landed on the furthest tile you planned to reach from your last count.
- **Optimize Each Step:** Every step you take is calculated to extend your reach, ensuring efficiency.

By keeping these memorable steps and key points in mind, you should be able to recall the essence of the jump algorithm anytime you need to.
  • Loading branch information
parvizmalik authored Nov 8, 2023
1 parent 4ea0a9b commit 6c8519e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Jump Game II.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function jump(nums) {
let jumps = 0,
currentEnd = 0,
currentFarthest = 0;
for (let i = 0; i < nums.length - 1; i++) {
currentFarthest = Math.max(currentFarthest, i + nums[i]);
if (i === currentEnd) {
jumps++;
currentEnd = currentFarthest;
}
}
return jumps;
}

// Example usage:
const A = [2, 3, 1, 1, 4];
console.log(jump(A)); // Output: 2

0 comments on commit 6c8519e

Please sign in to comment.