Timeline
Timeline
2025-10-02
init
Greedy, Dynamic Programming
Problem:
The first thing that came to my mind is dynamic programming
Let dp[i] represent the minimum number of jumps to reach i, -1 means unreachable. Then for 0 <= j < i, if j can reach i, i.e., nums[j] + j >= i and dp[j] != -1
If there is no such j that is reachable, then
1 |
|
This problem is actually a classic greedy problem. Looking from the end, the last position must have been jumped from some previous position, but there may be multiple such positions. Then we greedily choose the farthest one (because it’s one more jump anyway, choosing the one closer to the start will definitely reduce the number of jumps. This is because if two points can both reach the last position, with a being earlier and b being later, we will definitely choose a, since the steps to reach a are less than or equal to those to reach b). After choosing, the selected position becomes the new last position.
1 | class Solution { |
You can also modify it using the idea of the following problem
Official solution:
If we greedily search forward, each time finding the farthest reachable position, we can obtain the minimum number of jumps in linear time.
For example, for the array [2,3,1,2,4,2,3], the initial position is index 0. Starting from index 0, the farthest reachable index is 2. Among the positions reachable from index 0, the value at index 1 is 3, and starting from index 1 can reach a farther position, so the first step reaches index 1.
Starting from index 1, the farthest reachable index is 4. Among the positions reachable from index 1, the value at index 4 is 4, and starting from index 4 can reach a farther position, so the second step reaches index 4.

In the specific implementation, we maintain the maximum index position that can be reached currently, called the boundary. We traverse the array from left to right, and when we reach the boundary, we update the boundary and increase the jump count by 1.
When traversing the array, we do not visit the last element, because before visiting the last element, our boundary must be greater than or equal to the last position, otherwise we cannot jump to the last position. If we visit the last element, when the boundary is exactly the last position, we will add an ‘unnecessary jump count’, so we do not need to visit the last element.
1 | class Solution { |
leetcode hot 100 rewrite
1 |
|
