Minimize cost to traverse the Arrays with given Array switching cost
Given three arrays A[], B[] and C[] of size N each and two integers X and Y, the task is to, find the minimum cost to reach the end of the arrays with the following conditions:
- Select one of the three elements at every index.
- For switching from 1st array to 2nd or vice versa, extra X points are required and
- For switching from 2nd array to 3rd or vice versa, extra Y points are required.
- The first element (at index 0) must be selected from 1st array only.
Note: It’s not possible to switch from 1st to 3rd array directly or vice versa.
Examples:
Input: X = 6, Y = 3
A[] = { 30, 10, 40, 2, 30, 30 }
B[] = { 10, -5, 10, 50, 7, 18 }
C[] = { 4, 20, 7, 23, 2, 10 }
Output: 75
Explanation: We select this elements at index (0 to N-1).
A[] = { 30, 10, 40, 2, 30, 30 }
B[] = { 10, -5, 10, 50, 7, 18 }
C[] = { 4, 20, 7, 23, 2, 10 }
From index 0 we have to select 30. So Total cost to traverse up to index 0 is 30.
Now At index 1, 3 options (10, -5, 20). So select -5 from 2nd array.
So extra X (X = 6) cost required to shift from 1st array to 2nd.
So Total cost to traverse up to index 1 is 30 + 6 – 5 = 31.
At index 2, select 10. So Total cost to traverse upto index 2 is 31+10= 41.
At index 3, select 2 from 1st array,
So extra X (X = 6) points required to shift from 2nd array to 1st.
So Total cost to traverse upto index 3 is 41+6+2 = 49 and so on.
So, total cost = 30 + (6+(-5)) + 10 + (6+2) + (6+7) + (3+10) = 75
6 and 3 are the extra costs required to change array.Input: X = -5, Y = 4
A[] = { 30, 10, -15, 10, 50, 7 }
B[] = { 4, 20, 7, 23, 2, 18 }
C[] = { 30, 10, 40, 2, 30, 10 }
Output: 34
Approach: The problem can be solved based on the Greedy Approach based on the following idea:
At each index try to include the element from the array that will include the minimum cost. And also keep in mind that we can go from A[] to B[], B[] to C[], B[] to A[] and C[] to B[] not from A[] to C[] or C[] to A[].
Follow the below steps to implement the idea:
- Create a 2D array (say min_pnts[N][3]).
- Traverse the array from the last index to the first index and in each iteration:
- The value of min_pnts[i][0] depends on min_pnts[i+1][0] and min_pnts[i+1][1], value of min_pnts[i][1] depends on min_pnts[i+1][1], min_pnts[i+1][0] and min_pnts[i+1][2], value of min_pnts[i][2] depends on min_pnts[i+1][2] and min_pnts[i+1][1].
- Check which path required minimum points (i.e. with switching array or not).
- Store the minimum points required if we select the current index element from the 1st, 2nd, or 3rd array.
- Return the minimum points required to traverse the array.
Below is the implementation of the above approach.
C++
// C++ code to implement above approach #include <bits/stdc++.h> using namespace std; // Function to find minimum cost // to traverse till the end point int solve(vector< int > A, vector< int > B, vector< int > C, int N, int X, int Y) { // Initialize one 2D vector vector<vector< int > > min_pnts(3, vector< int >(N, 0)); min_pnts[0][N - 1] = A[N - 1]; min_pnts[1][N - 1] = B[N - 1]; min_pnts[2][N - 1] = C[N - 1]; // Start traversing the array // by using minimum points for ( int i = N - 2; i >= 0; i--) { min_pnts[0][i] = A[i] + min(min_pnts[0][i + 1], min_pnts[1][i + 1] + X); min_pnts[1][i] = B[i] + min({ min_pnts[0][i + 1] + X, min_pnts[1][i + 1], min_pnts[2][i + 1] + Y }); min_pnts[2][i] = C[i] + min(min_pnts[2][i + 1], min_pnts[1][i + 1] + Y); } return min_pnts[0][0]; } // Driver code int main() { int X = 6, Y = 3; vector< int > A = { 30, 10, 40, 2, 30, 30 }; vector< int > B = { 10, -5, 10, 50, 7, 18 }; vector< int > C = { 4, 20, 7, 23, 2, 10 }; // Function call cout << solve(A, B, C, A.size(), X, Y); return 0; } |
Java
// Java code to implement above approach import java.io.*; class GFG { // Function to find minimum cost // to traverse till the end point public static int solve( int A[], int B[], int C[], int N, int X, int Y) { // Initialize one 2D vector int min_pnts[][] = new int [ 3 ][N]; min_pnts[ 0 ][N - 1 ] = A[N - 1 ]; min_pnts[ 1 ][N - 1 ] = B[N - 1 ]; min_pnts[ 2 ][N - 1 ] = C[N - 1 ]; // Start traversing the array // by using minimum points for ( int i = N - 2 ; i >= 0 ; i--) { min_pnts[ 0 ][i] = A[i] + Math.min(min_pnts[ 0 ][i + 1 ], min_pnts[ 1 ][i + 1 ] + X); min_pnts[ 1 ][i] = B[i] + Math.min( min_pnts[ 0 ][i + 1 ] + X, Math.min(min_pnts[ 1 ][i + 1 ], min_pnts[ 2 ][i + 1 ] + Y)); min_pnts[ 2 ][i] = C[i] + Math.min(min_pnts[ 2 ][i + 1 ], min_pnts[ 1 ][i + 1 ] + Y); } return min_pnts[ 0 ][ 0 ]; } // Driver Code public static void main(String[] args) { int X = 6 , Y = 3 ; int A[] = { 30 , 10 , 40 , 2 , 30 , 30 }; int B[] = { 10 , - 5 , 10 , 50 , 7 , 18 }; int C[] = { 4 , 20 , 7 , 23 , 2 , 10 }; // Function call System.out.print(solve(A, B, C, A.length, X, Y)); } } // This code is contributed by Rohit Pradhan |
Python3
# python3 code to implement above approach # Function to find minimum cost # to traverse till the end point def solve(A, B, C, N, X, Y): # Initialize one 2D vector min_pnts = [[ 0 for _ in range (N)] for _ in range ( 3 )] min_pnts[ 0 ][N - 1 ] = A[N - 1 ] min_pnts[ 1 ][N - 1 ] = B[N - 1 ] min_pnts[ 2 ][N - 1 ] = C[N - 1 ] # Start traversing the array # by using minimum points for i in range (N - 2 , - 1 , - 1 ): min_pnts[ 0 ][i] = A[i] + min (min_pnts[ 0 ][i + 1 ], min_pnts[ 1 ][i + 1 ] + X) min_pnts[ 1 ][i] = B[i] + min ({min_pnts[ 0 ][i + 1 ] + X, min_pnts[ 1 ][i + 1 ], min_pnts[ 2 ][i + 1 ] + Y}) min_pnts[ 2 ][i] = C[i] + min (min_pnts[ 2 ][i + 1 ], min_pnts[ 1 ][i + 1 ] + Y) return min_pnts[ 0 ][ 0 ] # Driver code if __name__ = = "__main__" : X, Y = 6 , 3 A = [ 30 , 10 , 40 , 2 , 30 , 30 ] B = [ 10 , - 5 , 10 , 50 , 7 , 18 ] C = [ 4 , 20 , 7 , 23 , 2 , 10 ] # Function call print (solve(A, B, C, len (A), X, Y)) # This code is contributed by rakeshsahni |
C#
// C# code to implement above approach using System; class GFG { // Function to find minimum cost // to traverse till the end point static int solve( int [] A, int [] B, int [] C, int N, int X, int Y) { // Initialize one 2D vector int [, ] min_pnts = new int [3, N]; min_pnts[0, N - 1] = A[N - 1]; min_pnts[1, N - 1] = B[N - 1]; min_pnts[2, N - 1] = C[N - 1]; // Start traversing the array // by using minimum points for ( int i = N - 2; i >= 0; i--) { min_pnts[0, i] = A[i] + Math.Min(min_pnts[0, i + 1], min_pnts[1, i + 1] + X); min_pnts[1, i] = B[i] + Math.Min( min_pnts[0, i + 1] + X, Math.Min(min_pnts[1, i + 1], min_pnts[2, i + 1] + Y)); min_pnts[2, i] = C[i] + Math.Min(min_pnts[2, i + 1], min_pnts[1, i + 1] + Y); } return min_pnts[0, 0]; } // Driver Code public static void Main() { int X = 6, Y = 3; int [] A = { 30, 10, 40, 2, 30, 30 }; int [] B = { 10, -5, 10, 50, 7, 18 }; int [] C = { 4, 20, 7, 23, 2, 10 }; // Function call Console.Write(solve(A, B, C, A.Length, X, Y)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Function to find minimum cost // to traverse till the end point function solve(A, B, C, N, X, Y) { // Initialize one 2D vector var min_pnts = new Array(3); // Loop to create 2D array using 1D array for ( var i = 0; i < N; i++) { min_pnts[i] = new Array(3); } min_pnts[0][N - 1] = A[N - 1]; min_pnts[1][N - 1] = B[N - 1]; min_pnts[2][N - 1] = C[N - 1]; // Start traversing the array // by using minimum point for (let i = N - 2; i >= 0; i--) { min_pnts[0][i] = A[i] + Math.min(min_pnts[0][i + 1], min_pnts[1][i + 1] + X); min_pnts[1][i] = B[i] + Math.min( min_pnts[0][i + 1] + X, Math.min(min_pnts[1][i + 1], min_pnts[2][i + 1] + Y)); min_pnts[2][i] = C[i] + Math.min(min_pnts[2][i + 1], min_pnts[1][i + 1] + Y); } return min_pnts[0][0]; } // Driver Code let X = 6, Y = 3; let A = [ 30, 10, 40, 2, 30, 30 ]; let B = [ 10, -5, 10, 50, 7, 18 ]; let C = [ 4, 20, 7, 23, 2, 10 ]; // Function call document.write(solve(A, B, C, A.length, X, Y)); // This code is contributed by sanoy_62. </script> |
75
Time Complexity: O(N)
Auxiliary space: O(N)
Please Login to comment...