Fastest algorithm to hop through an array

后端 未结 10 1109
栀梦
栀梦 2021-02-05 16:52

Start with an array A of positive numbers. Start at index 0. From index i, you can move to index i+x for any x <= A[i]. The goal is to find the minimum number of moves needed

10条回答
  •  死守一世寂寞
    2021-02-05 17:14

    Treat the array of numbers as a graph and then the problem is equivalent to the Shortest Path Problem, which can be solved in O(|E|+|V|log|V|) time using Dijkstra's algorithm.

    E = of the numbers.

    V = # of numbers.

提交回复
热议问题