Fastest algorithm to hop through an array

后端 未结 10 1111
栀梦
栀梦 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.

    0 讨论(0)
  • 2021-02-05 17:16

    Use your basic idea, but start from the beginning instead and you can get O(n).

    The goal is to make a sequence (A_i1, A_i2, ..., A_ik, ...) such that

    1. positions 0,1,2,...,ik can be reached in k or fewer steps

    2. positions i(k-1)+1, i(k-1)+2, ..., ik cannot be reach in fewer than k steps

    The base case is easy:

    i0 = 0
    i1 = A[0]
    

    and the inductive part isn't too complicated:

    i(k+2) = max { A_(ik+1) + ik , A_(ik+1) + ik+1, ..., A_(i(k+1)) + i(k+1) }
    
    0 讨论(0)
  • 2021-02-05 17:19

    You could formulate it as a graph algorithm (really, what problem can't be?). Let the positions in the array be the vertices, and the possible destinations have an edge from each vertex. In your example, vertex 0 would have edges to 1 and 2, while vertex 1 would have edges to 2, 3, 4 and 5.

    There are several efficient graph search algorithms. For instance, Dijkstra's is O(|E| + |V|log|V|), and A* is O(log h*), which is better if you can come up with a good heuristic.

    0 讨论(0)
  • 2021-02-05 17:21

    You can convert the array into a graph and find the shortest path. Here is how the transformation from array to graph should work.

    Each array element is an node. And, based on the value in the array element a edge to drawn between the node to the other indices(nodes) we can jump to. Once we have this graph we can find shortest path, which is better than O(n^2).

    http://i.imgur.com/Ih3UP.png

    0 讨论(0)
  • 2021-02-05 17:22

    Dynamic programming solution:

    keep track for each element the smallest number of steps you can get there and from where you came. then just simply walk through the array and for each element update the available next positions (from i+1 till i+a[i]).

    { 2 , 4 , 1 , 2 , 3 , 2 , 4 , 2} 
      0
    
    { 2 , 4 , 1 , 2 , 3 , 2 , 4 , 2} 
      0   1   1 (num of steps)
          0   0 (source)
      ^         (current position)
    { 2 , 4 , 1 , 2 , 3 , 2 , 4 , 2} 
      0   1   1   2   2   2
          0   0   1   1   1
          ^
    { 2 , 4 , 1 , 2 , 3 , 2 , 4 , 2} 
      0   1   1   2   2   2
              ^
    etc...
    

    This is O(n+sum(a[i])) .. or a bit less, you don't have to go beyond the boundary of the array.

    0 讨论(0)
  • 2021-02-05 17:25

    My naive approach - going from the start, doing breath-first through all paths ( child nodes are A[i+1] .. A[i+n] ), saving found paths yo some array and then get the shortest paths. Of course all indexes i+n > length(A) are discarded. So it's upper bound is O(n*min(n,max(A[i=0..n])) + n) - in should less than quadratic in practice.

    0 讨论(0)
提交回复
热议问题