Minimum cost to reach end of array when a maximum jump of K index is allowed
Last Updated :
14 Oct, 2022
Given an array arr[] of N integers and an integer K, one can move from an index i to any other j if j ? i + k. The cost of moving from one index i to the other index j is abs(arr[i] – arr[j]). Initially, we start from the index 0 and we need to reach the last index i.e. N – 1. The task is to reach the last index in the minimum cost possible.
Examples:
Input: arr[] = {10, 30, 40, 50, 20}, k = 3
Output: 30
0 -> 1 -> 4
the total cost will be: |10-30| + |30-20| = 30
Input: arr[] = {40, 10, 20, 70, 80, 10}, k = 4
Output: 30
Approach 1: The problem can be solved using Dynamic Programming. We start from index 0 and we can visit any of the indexes from i+1 to i+k, hence the minimum cost of all the paths will be stored in dp[i]. Once we reach N-1, it will be our base case. Use memoization to memoize the states, so that we do not need to revisit the state again to reduce complexity.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int FindMinimumCost( int ind, int a[],
int n, int k, int dp[])
{
if (ind == (n - 1))
return 0;
else if (dp[ind] != -1)
return dp[ind];
else {
int ans = INT_MAX;
for ( int i = 1; i <= k; i++) {
if (ind + i < n)
ans = min(ans, abs (a[ind + i] - a[ind])
+ FindMinimumCost(ind + i, a,
n, k, dp));
else
break ;
}
return dp[ind] = ans;
}
}
int main()
{
int a[] = { 10, 30, 40, 50, 20 };
int k = 3;
int n = sizeof (a) / sizeof (a[0]);
int dp[n];
memset (dp, -1, sizeof dp);
cout << FindMinimumCost(0, a, n, k, dp);
return 0;
}
|
Java
import java.util.*;
class GfG
{
static int FindMinimumCost( int ind, int a[],
int n, int k, int dp[])
{
if (ind == (n - 1 ))
return 0 ;
else if (dp[ind] != - 1 )
return dp[ind];
else {
int ans = Integer.MAX_VALUE;
for ( int i = 1 ; i <= k; i++)
{
if (ind + i < n)
ans = Math.min(ans, Math.abs(a[ind + i] - a[ind]) +
FindMinimumCost(ind + i, a, n, k, dp));
else
break ;
}
return dp[ind] = ans;
}
}
public static void main(String[] args)
{
int a[] = { 10 , 30 , 40 , 50 , 20 };
int k = 3 ;
int n = a.length;
int dp[] = new int [n];
Arrays.fill(dp, - 1 );
System.out.println(FindMinimumCost( 0 , a, n, k, dp));
}
}
|
Python3
import sys
def FindMinimumCost(ind, a, n, k, dp):
if (ind = = (n - 1 )):
return 0
elif (dp[ind] ! = - 1 ):
return dp[ind]
else :
ans = sys.maxsize
for i in range ( 1 , k + 1 ):
if (ind + i < n):
ans = min (ans, abs (a[ind + i] - a[ind]) +
FindMinimumCost(ind + i, a, n, k, dp))
else :
break
dp[ind] = ans
return ans
if __name__ = = '__main__' :
a = [ 10 , 30 , 40 , 50 , 20 ]
k = 3
n = len (a)
dp = [ - 1 for i in range (n)]
print (FindMinimumCost( 0 , a, n, k, dp))
|
C#
using System;
class GfG
{
static int FindMinimumCost( int ind, int []a,
int n, int k, int []dp)
{
if (ind == (n - 1))
return 0;
else if (dp[ind] != -1)
return dp[ind];
else {
int ans = int .MaxValue;
for ( int i = 1; i <= k; i++)
{
if (ind + i < n)
ans = Math.Min(ans, Math.Abs(a[ind + i] - a[ind]) +
FindMinimumCost(ind + i, a, n, k, dp));
else
break ;
}
return dp[ind] = ans;
}
}
public static void Main()
{
int []a = { 10, 30, 40, 50, 20 };
int k = 3;
int n = a.Length;
int []dp = new int [n];
for ( int i = 0; i < n ; i++)
dp[i] = -1 ;
Console.WriteLine(FindMinimumCost(0, a, n, k, dp));
}
}
|
PHP
<?php
function FindMinimumCost( $ind , $a , $n , $k , $dp )
{
if ( $ind == ( $n - 1))
return 0;
else if ( $dp [ $ind ] != -1)
return $dp [ $ind ];
else
{
$ans = PHP_INT_MAX;
for ( $i = 1; $i <= $k ; $i ++)
{
if ( $ind + $i < $n )
$ans = min( $ans , abs ( $a [ $ind + $i ] - $a [ $ind ]) +
FindMinimumCost( $ind + $i , $a , $n , $k , $dp ));
else
break ;
}
return $dp [ $ind ] = $ans ;
}
}
$a = array (10, 30, 40, 50, 20 );
$k = 3;
$n = sizeof( $a );
$dp = array ();
$dp = array_fill (0, $n , -1);
echo (FindMinimumCost(0, $a , $n , $k , $dp ));
?>
|
Javascript
<script>
function FindMinimumCost(ind , a , n , k , dp) {
if (ind == (n - 1))
return 0;
else if (dp[ind] != -1)
return dp[ind];
else {
var ans = Number.MAX_VALUE;
for ( var i = 1; i <= k; i++) {
if (ind + i < n)
ans = Math.min(ans, Math.abs(a[ind + i] - a[ind])
+ FindMinimumCost(ind + i, a, n, k, dp));
else
break ;
}
return dp[ind] = ans;
}
}
var a = [ 10, 30, 40, 50, 20];
var k = 3;
var n = a.length;
var dp = Array(n).fill(-1);
document.write(FindMinimumCost(0, a, n, k, dp));
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(N)
Approach 2:
The second approach also requires the use of Dynamic programming. This approach is based on Bellman ford’s DP solution to the single-source shortest path. In Bellman’s ford SSSP, the main idea is to find the next vertex through minimizing on edges, we can do the same we minimize on abs values of two elements of an array to find the next index.
For solving any DP problems we first guess all the possible solutions to the subproblems and memoize them then choose the best solutions to the subproblem. we write Recurrence for the problem
Recurrence: DP(j) = min{DP(i) + abs(A[i] – A[j])} where i is in [0, N-1] and j is in [i + 1, j + k + 1], and k is number of jumps allowed. this can also be compared with relaxation in SSSP. We are going to relax every next approachable index.
// base case
memo[0] = 0;
for (i = 0 to N-1)
for (j = i+1 to i+k+1)
memo[j] = min(memo[j], memo[i] + abs(A[i] – A[j]));
Below is the implementation of the Bottom-up above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min( int a, int b) {
return (a > b) ? b : a;
}
int minCostJumpsDP(vector < int > & A, int k) {
int size = A.size();
vector < int > x(size, INT_MAX);
x[0] = 0;
for ( int i = 0; i < size; i++) {
for ( int j = i + 1; j < i + k + 1; j++) {
x[j] = min(x[j], x[i] + abs (A[i] - A[j]));
}
}
return x[size - 1];
}
int main()
{
vector < int > input { 83, 26, 37, 35, 33, 35, 56 };
cout << minCostJumpsDP(input, 3);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int min( int a, int b)
{
return (a > b) ? b : a;
}
static int minCostJumpsDP( int []A, int k)
{
int size = A.length;
int []x = new int [size];
Arrays.fill(x, Integer.MAX_VALUE);
x[ 0 ] = 0 ;
for ( int i = 0 ; i < size; i++)
{
for ( int j = i + 1 ;
j < i + k + 1 &&
j < size; j++)
{
x[j] = min(x[j], x[i] +
Math.abs(A[i] - A[j]));
}
}
return x[size - 1 ];
}
public static void main(String []args)
{
int []input = { 83 , 26 , 37 , 35 , 33 , 35 , 56 };
System.out.println(minCostJumpsDP(input, 3 ));
}
}
|
Python3
import sys
def minCostJumpsDP(A, k):
size = len (A)
x = [sys.maxsize] * (size)
x[ 0 ] = 0
for i in range (size):
j = i + 1
while j < i + k + 1 and j < size:
x[j] = min (x[j], x[i] + abs (A[i] - A[j]))
j + = 1
return x[size - 1 ]
if __name__ = = "__main__" :
input_ = [ 83 , 26 , 37 , 35 , 33 , 35 , 56 ]
print (minCostJumpsDP(input_, 3 ))
|
C#
using System;
class GFG
{
static int min( int a, int b)
{
return (a > b) ? b : a;
}
static int minCostJumpsDP( int []A, int k)
{
int size = A.Length;
int []x = new int [size];
for ( int i = 0; i < size; i++)
x[i] = int .MaxValue;
x[0] = 0;
for ( int i = 0; i < size; i++)
{
for ( int j = i + 1;
j < i + k + 1 &&
j < size; j++)
{
x[j] = min(x[j], x[i] +
Math.Abs(A[i] - A[j]));
}
}
return x[size - 1];
}
public static void Main(String []args)
{
int []input = { 83, 26, 37, 35, 33, 35, 56 };
Console.WriteLine(minCostJumpsDP(input, 3));
}
}
|
Javascript
<script>
function min(a, b)
{
return (a > b) ? b : a;
}
function minCostJumpsDP(A, k)
{
let size = A.length;
let x = new Array(size);
for (let i = 0; i < size; i++)
x[i] = Number.MAX_VALUE;
x[0] = 0;
for (let i = 0; i < size; i++)
{
for (let j = i + 1;
j < i + k + 1 &&
j < size; j++)
{
x[j] = min(x[j], x[i] +
Math.abs(A[i] - A[j]));
}
}
return x[size - 1];
}
let input = [ 83, 26, 37, 35, 33, 35, 56 ];
document.write(minCostJumpsDP(input, 3));
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...