Title Link: 55. Jumping game I
Title details:
Given a nonnegative integer array of , 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 skip 1 step from subscript 0 to subscript 1, and then skip 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.
Tips:
- 1 <= nums.length <= 3 * 10^4
- 0 <= nums[i] <= 10^5
The problem is still very simple. Through simple analysis, it is concluded that in this non negative integer array nums, as long as a 0 does not necessarily reach the last subscript from the first subscript of the array. If there is a 0, it does not necessarily reach the last subscript.
It should also be noted that when the array has only one bit, no matter how many elements of the initial subscript are, it must be true, because at this time, the first subscript of the array is also the last subscript of the array. If the array is greater than one bit and the element of the initial subscript is 0, it must be false. Because the next jump cannot be carried out at all, the last subscript cannot be reached.
At first, the code I wrote just judged whether I could reach the end point. Although it also reflected the idea of greed, it was not obvious, so I planned to continue to optimize it. (the code will not be released)
Let's use an example to briefly analyze how to use greed to analyze and optimize:
The array nums given in example 1 = [2,3,1,1,4];
Initially, we can jump two grids, and the range we can jump is [2,3,1,1,4]; (black bold indicates the current position and red bold indicates the maximum range that can be jumped). At this time, we choose the jumping position: 1 If we jump to the position of 1 (the array subscript is 2), the maximum range we can jump to is [2,3,1,1,4], and then we can jump to the position of 4 (the last subscript of the array) through the above steps; 2. However, if we choose to jump to the position of 3 (the array subscript is one), the maximum range we can jump to next is [2,3,1,1,4]. At this time, we find that we reach the end point after two steps, which is the direction we optimize: after each jump, find the point that can reach the farthest range in the next jump within the maximum range we can jump to at present, This point is the position we choose to jump at the current position, and then repeat the above operations until we can reach the end point within the jump range (the position of the last subscript of the array).
Modified code (c + +):
class Solution { public : bool canJump(vector<int>& nums) { int length=nums.size(); if(length==1)//If there is only one element, it must succeed { return true; } if(nums[0]==0)//If there is more than one element and the initial element is 0, it must fail { return false; } int max=nums[0],jmax;//max is the longest distance you can jump to, and jmax is the longest distance you can jump to within the current distance for(int i=0;i<max;)//Find the longest distance that can be reached by the next jump at the longest distance that can be reached by the current jump { if(i+nums[i]>=length-1)//If the current can jump to the last subscript of the array, it is successful { return true; } jmax=max; for(int j=i+1;j<=max;j++)//Find the point within the current range that can make the next jump farthest { if(j+nums[j]>=jmax)//If the next jump range of the point is less than the maximum range that can jump at present, you do not choose to jump to the point { jmax=j+nums[j]; i=j; } } max=jmax;//The maximum range of updates that can currently be skipped } return false; } };
After finishing this problem, I found that there was a second one, which was similar to the code of this problem, so I continued to do it.
Title Link: 45. Jumping game II
Title details:
Give you an array of nonnegative integers, {nums, and 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 hops 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
Tips:
- 1 <= nums.length <= 104
- 0 <= nums[i] <= 1000
It's almost the same as the previous question, and the minimum number of jump steps has been analyzed before, so just make a small change to the code. (it has been analyzed before, so the comments will not be written)
class Solution { public: int jump(vector<int>& nums) { int length=nums.size(); int max=nums[0],jmax; int count=0; if(nums[0]==0 || length==1)//When the element of the initial position is 0 or there is only one element, the minimum jump is 0 steps { return 0; } for(int i=0;i<max;) { jmax=max; if(i+nums[i]>=length-1) { count++; return count; } for(int j=i+1;j<=max;j++) { if(j+nums[j]>=jmax) { jmax=j+nums[j]; i=j; } } count++; max=jmax; } return count; } };