Minimum cost to reach end of array array when a maximum jump of K index is allowed | Python3 implementation of the approach ; for calculating the number of elements ; Allocating Memo table and initializing with INT_MAX ; Base case ; For every element relax every reachable element ie relax next k elements ; reaching next k element ; Relaxing the element ; return the last element in the array ; Driver Code
import sys NEW_LINE def minCostJumpsDP ( A , k ) : NEW_LINE INDENT size = len ( A ) NEW_LINE x = [ sys . maxsize ] * ( size ) NEW_LINE x [ 0 ] = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT j = i + 1 NEW_LINE while j < i + k + 1 and j < size : NEW_LINE INDENT x [ j ] = min ( x [ j ] , x [ i ] + abs ( A [ i ] - A [ j ] ) ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return x [ size - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input_ = [ 83 , 26 , 37 , 35 , 33 , 35 , 56 ] NEW_LINE print ( minCostJumpsDP ( input_ , 3 ) ) NEW_LINE DEDENT