Algorithm (5) Jump Game

Source: Internet
Author: User

Title: An array of non-negative numbers, each element of an array represents the maximum number of steps that you can span, starting at 0, and asking, can you just tune to the last of the array!

Https://leetcode.com/problems/jump-game/#/description

train of thought 1: from the tail record each element can reach the end, Algorithm complexity O (n*n) "At that time to come up with this algorithm, but also think good, but Leetcode said algorithm timeout Ah, was divided into minutes to teach a man"

Idea 2: a deep understanding of the data structures in this entire array! (ref.: http://blog.csdn.net/linhuanmars/article/details/21354751), first understand a problem: as long as the array does not contain 0, then this array must be able to jump past ! ==> got a conclusion: as long as you can jump out of the array, then we will be able to jump to the last position ! So just look at the beginning of the first position in the array, and the farthest you can jump to! Previous doubts include: 1 How do you know how far you can go from the first subscript? Do we not need to recursion all the steps on this road possible ? In case of a 0 on the road, that does not directly hope that the death penalty, we have to step back, and then ask for less to go this way ah! This is the crux of the previous problem, is a linear thinking! So we have the algorithm in the Reference Web page: We also record the global optimal and local optimal . Global optimal, real-time update, the global optimal between each node, is now reachable to know! Well, uh, well, there's a lot of stuff to analyze, and these are the keys to solving ideas. So long as the nodes within this range reach farther afield, the furthest point of the global should be updated!

Answer :

Https://github.com/honpey/codebox/blob/master/leetcode/array/p55.cpp

Algorithm (5) Jump Game

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.