Is it easy to use dp if the minimum number of steps is required?
In my first practice, when I found a location that can be extended from it to a further location, I updated the number of steps at this location and the farthest location, and the result timed out. In fact, this is not only timeout, but also error. Because many points in the distance can be reached in the previous step. In fact, only new points that can be reached should be updated, and which points are new points that can be reached? Assume that the point that can be extended this time is I, and its forward step is A [I]. The farthest point that can be reached last time is mmax, the new points that can be reached should be the locations between (mmax, I + A [I. The time complexity suddenly becomes linear.
Class Solution {public: int jump (int A [], int n) {if (n <= 1) return 0; vector <int> steps (n, INT_MAX ); steps [0] = 0; int mmax = 0; for (int I = 0; I <n-1; I ++) {if (I + A [I]> mmax) {for (int j = mmax + 1; j <= I + A [I] & j <n; j ++) steps [j] = steps [I] + 1; if (I + A [I]> = N-1) steps [n-1] = min (steps [n-1], steps [I] + 1); mmax = I + A [I] ;}} return steps [n-1] ;}};