Fastest Frog Jump
A[0] = 1
A[1] = 3
A[2] = 1
A[3] = 4
A[4] = 2
A[5] = 5int solution(int A[], int N, int X, int D);import queue
def fastest_frog_jump(leaf_times, jump_len, target):
# if the frog can jump across in one jump
if jump_len >= target:
return 0
# if the frog cannot jump across
if jump_len == 0 or not leaf_times:
return -1
# datastructure to contain the positions of the leafs
pq = queue.PriorityQueue()
frog_pos = 0
for time, leaf_loc in enumerate(leaf_times):
# check if the frog is currently within reach of target
if frog_pos + jump_len >= target:
return time
# if the leaf falls somewhere out in front of the frog,
# and whether or not it can current take the leaf or not
# will put it in the pq, and have it work it out
# it may be that the frog cannot take the leaf but in the
# future the leaf may be useful
if leaf_loc > frog_pos:
pq.put(leaf_loc)
# otherwise the leaf gets put behind the frog, in which case
# it can safely be ignored (it isn't getting us any closer)
# now just continue jumping on the next leaf, as far as
# the frog can
while not pq.empty():
# these leafs must be in front of the frog (always)
# careful to not actually remove from the pq till jump
closest_leaf = pq.queue[0]
if frog_pos + jump_len >= closest_leaf:
frog_pos = closest_leaf
# remove it from the queue since the
# frog was able to jump to it
pq.get()
# check if we can get to the end
if frog_pos + jump_len >= target:
return time
# otherwise we cannot jump to the closest leaf
else:
break
# at this point the frog cannot jump across
return -1
# note: time begins at 0
print(fastest_frog_jump([1,5,3,5,5,5], 2, 7)) # 2
print(fastest_frog_jump([2,1,0,1,2,3], 1, 4)) # 5
print(fastest_frog_jump([2,1,0,1,2,3], 1, 5)) # -1
print(fastest_frog_jump([6,5,4,3,2,1], 1, 7)) # 5 # (worst-case example)
print(fastest_frog_jump([1,3,1,4,2,5], 3, 7)) # 3
print(fastest_frog_jump([1,2,3,4,5,6], 1, 7)) # 5Last updated
