# Force buckle 55 Jumping game

Time：2022-5-26

## 55. Jumping game

55. Jumping game
Difficulty: medium
Given an array of nonnegative integers, {nums, you are initially at the first subscript of the array.

Each element in the array represents the maximum length you can jump at that position.

Judge whether you can reach the last subscript.

** **

Example 1:

Input: num = [2,3,1,1,4]
Output: true
Explanation: you can jump 1 step first, from subscript 0 to subscript 1, and then jump 3 steps from subscript 1 to the last subscript.
Example 2:

Input: num = [3,2,1,0,4]
Output: false
Explanation: in any case, you will always reach the position with subscript 3. However, the maximum jump length of this subscript is 0, so it is never possible to reach the last subscript.

analysis:
Using the greedy method, traverse each element of the array, record the current subscript and the corresponding element, and determine the maximum distance that can be reached. If it is greater than the current maximum distance, it will be updated, otherwise it will remain unchanged.

Take example 1 in the title
[2, 3, 1, 1, 4]
For example:

At the beginning, at position 0, the maximum length we can jump is 2, so the farthest reachable position is updated to 2;

We traverse to position 1. Since 1 ≤ 2, position 1 can be reached. We use 1 plus the maximum length it can jump 3 to update the farthest reachable position to 4. Since 4 is greater than or equal to the last position 4, we directly return true.

Let’s take another look at example 2 in the title

[3, 2, 1, 0, 4]
At the beginning, at position 0, the maximum length we can jump is 3, so the farthest reachable position is updated to 3;

We traverse to position 1. Since 1 ≤ 3, position 1 can be reached. Plus the maximum length 2 it can jump, we get 3, which does not exceed the farthest reachable position;

Position 2 and position 3 are the same, and the farthest reachable position will not be updated;

We traverse to position 4. Since 4 > 3, position 4 is unreachable, so we don’t consider the maximum length it can jump.

After the traversal, position 4 is still unreachable, so we return false.

``````class Solution {
public:
bool canJump(vector& nums) {
int maxlen=0;
int a=0;
for(int i=0;i=nums.size()-1)
return true;
}
}
return false;

}
};``````
1. Jumping game II
45. Jumping game II
Give you an array of nonnegative integers, {nums, where you are initially in the first position of the array.

Each element in the array represents the maximum length you can jump at that position.

Your goal is to use the least number of jumps to reach the last position of the array.

Suppose you can always reach the last position of the array.

** **

Example 1:

Input: num = [2,3,1,1,4]
Output: 2
Explanation: the minimum number of jumps to the last position is 2.
**Jump from the position with subscript 0 to the position with subscript 1, jump 1 # steps, and then jump 3 # steps to the last position of the array**
Example 2:

Input: num = [2,3,0,1,4]
Output: 2

thinking
1. If the jumping distance of a certain grid as the take-off point is 3, it means that the following three grids can be used as the take-off point.
** 11. You can try to jump once for each grid that can be used as the take-off point, and constantly update the farthest distance you can jump**

2. If taking off from this take-off point is called the first jump, then taking off from the back three squares can be called the second jump.

3. Therefore, at the end of a jump, starting from the next grid and jumping to the farthest distance now are the starting points of the next jump.
** 31. Simulate each jump with a for loop**
** 32. After one jump, update the range of the next take-off point**
** 33. Jump in the new range, and the update can jump to the farthest distance**

If you get the number of jumps, you get the result.

``````class Solution {
public:
int jump(vector& nums) {
int res=0;
int end=0;   // Record where the next hop can reach
for(int i=0;i=nums.size()-1)
return res;
int maxlen=0;
For (int j = I; J < = end; j + +) // the maximum position that can be reached from the current to end is the maximum position that can be reached in the next hop
{
maxlen=max(maxlen,j+nums[j]);
}
end=maxlen;
res++;
}
return res;
}
};``````

## Two sections of Perl script code (array application and say usage)

1、 Array application Copy codeThe code is as follows: #!/usr/bin env perl @zeng = `top -b -n 1`; foreach (@zeng) { print “\$_\n”; } 2、 Usage of say: Auto wrap Copy codeThe code is as follows: #!/user/bin env perluse 5.010; # Indicates that Perl version 5.10 is used. say “Hello World!”; my @fred = above_average(1..10); […]