Leetcode first _ Jump Game II

Source: Internet
Author: User

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] ;}};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.