Fastest algorithm to hop through an array

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

提交回复
热议问题