A car factory has two assembly lines, each containing n stations. Every station performs a specific task such as engine fitting, body fitting, painting, etc. The stations at the same position on both assembly lines perform the same type of task.
You are given:
A 2D array a[][] of size 2 * n, where a[i][j] represents the time required to process station j on assembly line i.
A 2D array T[][] of size 2 * n where T[0][j] represents the time required to switch from assembly line 1 to assembly line 2, and T[1][j] represents the time required to switch from assembly line 2 toassembly line 1, both between stations j-1 and j.
Entry times e[], where e[i] is the time required to enter assembly line i.
Exit times x[], where x[i] is the time required to exit assembly line i.
A car chassis must pass through all n stations in order, starting from either assembly line, and may switch between lines at any station while incurring the corresponding transfer time.
Determine the minimum total time required to manufacture the car chassis.
Example:
Input: a[2][] =[[4, 5, 3, 2], [2, 10, 1, 4]], T[2][] = [[0,7, 4, 5], [0,9, 2, 8]], e[2] = [10,12], x[2] = [18,7] Output: 35 Explanation: According to the TC, this would be the following diagram. The bold line shows the path covered by the car chassis for given input values. So the minimum time taken by the car is 35.
[Naive Approach] Using Recursion Approach - O(2 ^ n) Time and O(n) Space
We have two choices: either continue on the same assembly line or switch to the other line by paying the transfer cost. The recursive function explores both possibilities for each station and calculates the minimum total time needed to reach the end. The base case occurs at the last station, where we simply add the exit time of the current assembly line. Finally, we take the minimum time obtained by starting from either of the two assembly lines.
Start the car from both assembly lines separately.
At each station, either stay on the same line or switch to the other line.
If switching lines, add the corresponding transfer time.
Recursively calculate the minimum cost for both choices.
At the last station, add the exit time of the current assembly line.
Return the minimum total time obtained from both starting lines.
C++
#include<algorithm>#include<iostream>#include<vector>usingnamespacestd;// Recursive functionintsolve(vector<vector<int>>&a,vector<vector<int>>&T,intcurrentLine,intcurrentStation,vector<int>&x,intn){// Base Case:// If we are at the last station,// add exit time of the current lineif(currentStation==n-1){returnx[currentLine];}// Option 1:// Continue on the same assembly lineintsameLine=solve(a,T,currentLine,currentStation+1,x,n)+a[currentLine][currentStation+1];// Option 2:// Switch to the other assembly lineintchangeLine=solve(a,T,!currentLine,currentStation+1,x,n)+a[!currentLine][currentStation+1]+T[currentLine][currentStation+1];// Return minimum of both choicesreturnmin(sameLine,changeLine);}// Function to find minimum assembly timeintcarAssembly(vector<vector<int>>&a,vector<vector<int>>&T,vector<int>&e,vector<int>&x){intn=a[0].size();// Start from Assembly Line 0intline0=solve(a,T,0,0,x,n)+e[0]+a[0][0];// Start from Assembly Line 1intline1=solve(a,T,1,0,x,n)+e[1]+a[1][0];// Return overall minimum timereturnmin(line0,line1);}intmain(){vector<vector<int>>a={{4,5,3,2},{2,10,1,4}};vector<vector<int>>T={{0,7,4,5},{0,9,2,8}};vector<int>e={10,12};vector<int>x={18,7};intans=carAssembly(a,T,e,x);cout<<ans<<endl;return0;}
Java
importjava.util.*;// Main ClasspublicclassGFG{// Recursive functionstaticintsolve(int[][]a,int[][]T,intcurrentLine,intcurrentStation,int[]x,intn){// Base Case:// If we are at the last stationif(currentStation==n-1){returnx[currentLine];}// Option 1:// Continue on same lineintsameLine=solve(a,T,currentLine,currentStation+1,x,n)+a[currentLine][currentStation+1];// Option 2:// Switch to another lineintchangeLine=solve(a,T,1-currentLine,currentStation+1,x,n)+a[1-currentLine][currentStation+1]+T[currentLine][currentStation+1];// Return minimumreturnMath.min(sameLine,changeLine);}// Function to find minimum assembly timepublicstaticintcarAssembly(intn,int[][]a,int[][]T,int[]e,int[]x){// Start from line 0intline0=solve(a,T,0,0,x,n)+e[0]+a[0][0];// Start from line 1intline1=solve(a,T,1,0,x,n)+e[1]+a[1][0];// Return minimum timereturnMath.min(line0,line1);}publicstaticvoidmain(String[]args){intn=4;int[][]a={{4,5,3,2},{2,10,1,4}};int[][]T={{0,7,4,5},{0,9,2,8}};int[]e={10,12};int[]x={18,7};intans=carAssembly(n,a,T,e,x);System.out.println(ans);}}
Python
# Recursive functiondefsolve(a,T,currentLine,currentStation,x,n):# Base Case:# If we are at the last stationifcurrentStation==n-1:returnx[currentLine]# Option 1:# Continue on same linesameLine=solve(a,T,currentLine,currentStation+1,x,n)+a[currentLine][currentStation+1]# Option 2:# Switch to another linechangeLine=solve(a,T,1-currentLine,currentStation+1,x,n)+ \
a[1-currentLine][currentStation+1]+ \
T[currentLine][currentStation+1]# Return minimumreturnmin(sameLine,changeLine)# Function to find minimum assembly timedefcarAssembly(n,a,T,e,x):# Start from line 0line0=(solve(a,T,0,0,x,n)+e[0]+a[0][0])# Start from line 1line1=(solve(a,T,1,0,x,n)+e[1]+a[1][0])# Return minimum timereturnmin(line0,line1)# Driver Codeif__name__=="__main__":n=4a=[[4,5,3,2],[2,10,1,4]]T=[[0,7,4,5],[0,9,2,8]]e=[10,12]x=[18,7]ans=carAssembly(n,a,T,e,x)print(ans)
C#
usingSystem;classGFG{// Recursive functionstaticintSolve(int[][]a,int[][]T,intcurrentLine,intcurrentStation,int[]x,intn){// Base Case:// If we are at the last stationif(currentStation==n-1){returnx[currentLine];}// Option 1:// Continue on same lineintsameLine=Solve(a,T,currentLine,currentStation+1,x,n)+a[currentLine][currentStation+1];// Option 2:// Switch to another lineintchangeLine=Solve(a,T,1-currentLine,currentStation+1,x,n)+a[1-currentLine][currentStation+1]+T[currentLine][currentStation+1];// Return minimumreturnMath.Min(sameLine,changeLine);}// Function to find minimum assembly timestaticintcarAssembly(int[][]a,int[][]T,int[]e,int[]x){intn=a[0].Length;// Start from line 0intline0=Solve(a,T,0,0,x,n)+e[0]+a[0][0];// Start from line 1intline1=Solve(a,T,1,0,x,n)+e[1]+a[1][0];// Return minimum timereturnMath.Min(line0,line1);}staticvoidMain(){int[][]a=newint[][]{newint[]{4,5,3,2},newint[]{2,10,1,4}};int[][]T=newint[][]{newint[]{0,7,4,5},newint[]{0,9,2,8}};int[]e={10,12};int[]x={18,7};intans=carAssembly(a,T,e,x);Console.WriteLine(ans);}}
JavaScript
// Recursive functionfunctionsolve(a,T,currentLine,currentStation,x,n){// Base Case:// If we are at the last stationif(currentStation===n-1){returnx[currentLine];}// Option 1:// Continue on same lineletsameLine=solve(a,T,currentLine,currentStation+1,x,n)+a[currentLine][currentStation+1];// Option 2:// Switch to another lineletchangeLine=solve(a,T,1-currentLine,currentStation+1,x,n)+a[1-currentLine][currentStation+1]+T[currentLine][currentStation+1];// Return minimumreturnMath.min(sameLine,changeLine);}// Function to find minimum assembly timefunctioncarAssembly(a,T,e,x){letn=a[0].length;// Start from line 0letline0=solve(a,T,0,0,x,n)+e[0]+a[0][0];// Start from line 1letline1=solve(a,T,1,0,x,n)+e[1]+a[1][0];// Return minimum timereturnMath.min(line0,line1);}// Driver Codeleta=[[4,5,3,2],[2,10,1,4]];letT=[[0,7,4,5],[0,9,2,8]];lete=[10,12];letx=[18,7];letans=carAssembly(a,T,e,x);console.log(ans);
Output
35
Consider the following example: line = 2, stations = 3
Explanation using recursive tree(highlighted states showing overlapping sub-problems)
[Expected Approach - 1] Using Memoization(DP) - O(n) Time and O(n) Space
The above recursive solution contains overlapping subproblems because the same state gets solved multiple times. We use Memoization (Dynamic Programming) and store the result of each state in a dp array. Whenever the same state is encountered again, we directly return the stored value instead of recalculating it.
Create a dp array initialized with -1 to store computed states.
Start recursion from both assembly lines at station 0.
For each station, either stay on the same line or switch to the other line.
If the current state is already present in dp, return the stored value.
Store the minimum cost of both choices in the dp array.
At the last station, add the exit time and return the minimum overall assembly time.
C++
#include<algorithm>#include<iostream>#include<vector>usingnamespacestd;// Recursive + Memoization functionintsolve(vector<vector<int>>&a,vector<vector<int>>&T,intcurrentLine,intcurrentStation,vector<int>&x,intn,vector<vector<int>>&dp){// Base Case:// If we are at the last station,// add exit time of current lineif(currentStation==n-1){returnx[currentLine];}// If already computedif(dp[currentLine][currentStation]!=-1){returndp[currentLine][currentStation];}// Option 1:// Continue on same assembly lineintsameLine=solve(a,T,currentLine,currentStation+1,x,n,dp)+a[currentLine][currentStation+1];// Option 2:// Switch to the other assembly lineintchangeLine=solve(a,T,!currentLine,currentStation+1,x,n,dp)+a[!currentLine][currentStation+1]+T[currentLine][currentStation+1];// Store and return minimum answerreturndp[currentLine][currentStation]=min(sameLine,changeLine);}// Function to find minimum assembly timeintcarAssembly(vector<vector<int>>&a,vector<vector<int>>&T,vector<int>&e,vector<int>&x){intn=a[0].size();// DP array initialized with -1vector<vector<int>>dp(2,vector<int>(n,-1));// Start from Assembly Line 0intline0=solve(a,T,0,0,x,n,dp)+e[0]+a[0][0];// Reset DP arraydp=vector<vector<int>>(2,vector<int>(n,-1));// Start from Assembly Line 1intline1=solve(a,T,1,0,x,n,dp)+e[1]+a[1][0];// Return minimum timereturnmin(line0,line1);}intmain(){vector<vector<int>>a={{4,5,3,2},{2,10,1,4}};vector<vector<int>>T={{0,7,4,5},{0,9,2,8}};vector<int>e={10,12};vector<int>x={18,7};intans=carAssembly(a,T,e,x);cout<<ans<<endl;return0;}
Java
importjava.util.*;// Main ClasspublicclassGFG{// Recursive + Memoization functionstaticintsolve(int[][]a,int[][]T,intcurrentLine,intcurrentStation,int[]x,intn,int[][]dp){// Base Case:// If we are at the last stationif(currentStation==n-1){returnx[currentLine];}// If already computedif(dp[currentLine][currentStation]!=-1){returndp[currentLine][currentStation];}// Option 1:// Continue on same lineintsameLine=solve(a,T,currentLine,currentStation+1,x,n,dp)+a[currentLine][currentStation+1];// Option 2:// Switch to another lineintchangeLine=solve(a,T,1-currentLine,currentStation+1,x,n,dp)+a[1-currentLine][currentStation+1]+T[currentLine][currentStation+1];// Store and return minimum answerreturndp[currentLine][currentStation]=Math.min(sameLine,changeLine);}// Function to find minimum assembly timepublicstaticintcarAssembly(intn,int[][]a,int[][]T,int[]e,int[]x){// DP array initialized with -1int[][]dp=newint[2][n];for(inti=0;i<2;i++){Arrays.fill(dp[i],-1);}// Start from line 0intline0=solve(a,T,0,0,x,n,dp)+e[0]+a[0][0];// Reset DP for second starting linefor(inti=0;i<2;i++){Arrays.fill(dp[i],-1);}// Start from line 1intline1=solve(a,T,1,0,x,n,dp)+e[1]+a[1][0];// Return minimum timereturnMath.min(line0,line1);}publicstaticvoidmain(String[]args){intn=4;int[][]a={{4,5,3,2},{2,10,1,4}};int[][]T={{0,7,4,5},{0,9,2,8}};int[]e={10,12};int[]x={18,7};intans=carAssembly(n,a,T,e,x);System.out.println(ans);}}
Python
# Recursive + Memoization functiondefsolve(a,T,currentLine,currentStation,x,n,dp):# Base Case:# If we are at the last stationifcurrentStation==n-1:returnx[currentLine]# If already computedifdp[currentLine][currentStation]!=-1:returndp[currentLine][currentStation]# Option 1:# Continue on same linesameLine=solve(a,T,currentLine,currentStation+1,x,n,dp)+a[currentLine][currentStation+1]# Option 2:# Switch to another linechangeLine=solve(a,T,1-currentLine,currentStation+1,x,n,dp)+ \
a[1-currentLine][currentStation+1]+ \
T[currentLine][currentStation+1]# Store and return minimum answerdp[currentLine][currentStation]=min(sameLine,changeLine)returndp[currentLine][currentStation]# Function to find minimum assembly timedefcarAssembly(n,a,T,e,x):# DP array initialized with -1dp=[[-1for_inrange(n)]for_inrange(2)]# Start from line 0line0=solve(a,T,0,0,x,n,dp)+e[0]+a[0][0]# Reset DP for second starting linedp=[[-1for_inrange(n)]for_inrange(2)]# Start from line 1line1=solve(a,T,1,0,x,n,dp)+e[1]+a[1][0]# Return minimum timereturnmin(line0,line1)# Driver Codeif__name__=="__main__":n=4a=[[4,5,3,2],[2,10,1,4]]T=[[0,7,4,5],[0,9,2,8]]e=[10,12]x=[18,7]ans=carAssembly(n,a,T,e,x)print(ans)
C#
usingSystem;classGFG{// Recursive + Memoization functionstaticintSolve(int[][]a,int[][]T,intcurrentLine,intcurrentStation,int[]x,intn,int[][]dp){// Base Case:// If we are at the last stationif(currentStation==n-1){returnx[currentLine];}// If already computedif(dp[currentLine][currentStation]!=-1){returndp[currentLine][currentStation];}// Option 1:// Continue on same lineintsameLine=Solve(a,T,currentLine,currentStation+1,x,n,dp)+a[currentLine][currentStation+1];// Option 2:// Switch to another lineintchangeLine=Solve(a,T,1-currentLine,currentStation+1,x,n,dp)+a[1-currentLine][currentStation+1]+T[currentLine][currentStation+1];// Store and return minimum answerdp[currentLine][currentStation]=Math.Min(sameLine,changeLine);returndp[currentLine][currentStation];}// Function to find minimum assembly timestaticintcarAssembly(int[][]a,int[][]T,int[]e,int[]x){intn=a[0].Length;// DP array initialized with -1int[][]dp=newint[2][];for(inti=0;i<2;i++){dp[i]=newint[n];for(intj=0;j<n;j++){dp[i][j]=-1;}}// Start from line 0intline0=Solve(a,T,0,0,x,n,dp)+e[0]+a[0][0];// Reset DP for second starting linefor(inti=0;i<2;i++){for(intj=0;j<n;j++){dp[i][j]=-1;}}// Start from line 1intline1=Solve(a,T,1,0,x,n,dp)+e[1]+a[1][0];// Return minimum timereturnMath.Min(line0,line1);}staticvoidMain(){int[][]a=newint[][]{newint[]{4,5,3,2},newint[]{2,10,1,4}};int[][]T=newint[][]{newint[]{0,7,4,5},newint[]{0,9,2,8}};int[]e={10,12};int[]x={18,7};intans=carAssembly(a,T,e,x);Console.WriteLine(ans);}}
JavaScript
// Recursive + Memoization functionfunctionsolve(a,T,currentLine,currentStation,x,n,dp){// Base Case:// If we are at the last stationif(currentStation===n-1){returnx[currentLine];}// If already computedif(dp[currentLine][currentStation]!==-1){returndp[currentLine][currentStation];}// Option 1:// Continue on same lineletsameLine=solve(a,T,currentLine,currentStation+1,x,n,dp)+a[currentLine][currentStation+1];// Option 2:// Switch to another lineletchangeLine=solve(a,T,1-currentLine,currentStation+1,x,n,dp)+a[1-currentLine][currentStation+1]+T[currentLine][currentStation+1];// Store and return minimum answerdp[currentLine][currentStation]=Math.min(sameLine,changeLine);returndp[currentLine][currentStation];}// Function to find minimum assembly timefunctioncarAssembly(a,T,e,x){letn=a[0].length;// DP array initialized with -1letdp=Array.from({length:2},()=>Array(n).fill(-1));// Start from line 0letline0=solve(a,T,0,0,x,n,dp)+e[0]+a[0][0];// Reset DP for second starting linedp=Array.from({length:2},()=>Array(n).fill(-1));// Start from line 1letline1=solve(a,T,1,0,x,n,dp)+e[1]+a[1][0];// Return minimum timereturnMath.min(line0,line1);}// Driver Codeleta=[[4,5,3,2],[2,10,1,4]];letT=[[0,7,4,5],[0,9,2,8]];lete=[10,12];letx=[18,7];letans=carAssembly(a,T,e,x);console.log(ans);
Output
35
[Expected Approach - 2] Using Bottom Up(DP) - O(n) Time and O(n) Space
In the Bottom-Up DP approach, we build the solution iteratively instead of using recursion. dp[line][i] stores the minimum time required to reach station i on a particular assembly line. For every station, we either continue on the same line or switch from the other line by paying the transfer cost, and store the minimum of both choices. Finally, we add the exit times and return the minimum overall assembly time.
Create a dp table where dp[line][i] stores the minimum time to reach station i on a particular assembly line.
Initialize the first station of both lines using entry time and station processing time.
Traverse all stations from left to right.
For every station, compute the minimum cost by either staying on the same line or switching from the other line.
Store the minimum value in the dp table for both assembly lines.
Add exit times at the last station and return the minimum overall assembly time.
C++
#include<algorithm>#include<iostream>#include<vector>usingnamespacestd;// Function to find minimum assembly timeintcarAssembly(vector<vector<int>>&a,vector<vector<int>>&T,vector<int>&e,vector<int>&x){intn=a[0].size();// dp[0][i] -> Minimum time to reach station i on line 0// dp[1][i] -> Minimum time to reach station i on line 1vector<vector<int>>dp(2,vector<int>(n));// Base Case:// Add entry time and first station timedp[0][0]=e[0]+a[0][0];dp[1][0]=e[1]+a[1][0];// Fill DP tablefor(inti=1;i<n;i++){// Stay on same line OR switch from other linedp[0][i]=min(dp[0][i-1]+a[0][i],dp[1][i-1]+T[1][i]+a[0][i]);dp[1][i]=min(dp[1][i-1]+a[1][i],dp[0][i-1]+T[0][i]+a[1][i]);}// Add exit timesreturnmin(dp[0][n-1]+x[0],dp[1][n-1]+x[1]);}intmain(){vector<vector<int>>a={{4,5,3,2},{2,10,1,4}};vector<vector<int>>T={{0,7,4,5},{0,9,2,8}};vector<int>e={10,12};vector<int>x={18,7};intans=carAssembly(a,T,e,x);cout<<ans<<endl;return0;}
Java
importjava.util.*;// Main ClasspublicclassGFG{// Function to find minimum assembly timepublicstaticintcarAssembly(intn,int[][]a,int[][]T,int[]e,int[]x){// dp[0][i] -> Minimum time to reach station i on// line 0 dp[1][i] -> Minimum time to reach station// i on line 1int[][]dp=newint[2][n];// Base Case:// Add entry time and first station timedp[0][0]=e[0]+a[0][0];dp[1][0]=e[1]+a[1][0];// Fill DP tablefor(inti=1;i<n;i++){// Stay on same line OR switch from other linedp[0][i]=Math.min(dp[0][i-1]+a[0][i],dp[1][i-1]+T[1][i]+a[0][i]);dp[1][i]=Math.min(dp[1][i-1]+a[1][i],dp[0][i-1]+T[0][i]+a[1][i]);}// Add exit timesreturnMath.min(dp[0][n-1]+x[0],dp[1][n-1]+x[1]);}publicstaticvoidmain(String[]args){intn=4;int[][]a={{4,5,3,2},{2,10,1,4}};int[][]T={{0,7,4,5},{0,9,2,8}};int[]e={10,12};int[]x={18,7};intans=carAssembly(n,a,T,e,x);System.out.println(ans);}}
Python
# Function to find minimum assembly timedefcarAssembly(n,a,T,e,x):# dp[0][i] -> Minimum time to reach station i on line 0# dp[1][i] -> Minimum time to reach station i on line 1dp=[[0for_inrange(n)]for_inrange(2)]# Base Case:# Add entry time and first station timedp[0][0]=e[0]+a[0][0]dp[1][0]=e[1]+a[1][0]# Fill DP tableforiinrange(1,n):# Stay on same line OR switch from other linedp[0][i]=min(dp[0][i-1]+a[0][i],dp[1][i-1]+T[1][i]+a[0][i])dp[1][i]=min(dp[1][i-1]+a[1][i],dp[0][i-1]+T[0][i]+a[1][i])# Add exit timesreturnmin(dp[0][n-1]+x[0],dp[1][n-1]+x[1])# Driver Codeif__name__=="__main__":n=4a=[[4,5,3,2],[2,10,1,4]]T=[[0,7,4,5],[0,9,2,8]]e=[10,12]x=[18,7]ans=carAssembly(n,a,T,e,x)print(ans)
C#
usingSystem;classGFG{// Function to find minimum assembly timestaticintcarAssembly(int[][]a,int[][]T,int[]e,int[]x){intn=a[0].Length;// dp[0][i] -> Minimum time to reach station i on// line 0 dp[1][i] -> Minimum time to reach station// i on line 1int[][]dp=newint[2][];dp[0]=newint[n];dp[1]=newint[n];// Base Case:// Add entry time and first station timedp[0][0]=e[0]+a[0][0];dp[1][0]=e[1]+a[1][0];// Fill DP tablefor(inti=1;i<n;i++){// Stay on same line OR switch from other linedp[0][i]=Math.Min(dp[0][i-1]+a[0][i],dp[1][i-1]+T[1][i]+a[0][i]);dp[1][i]=Math.Min(dp[1][i-1]+a[1][i],dp[0][i-1]+T[0][i]+a[1][i]);}// Add exit timesreturnMath.Min(dp[0][n-1]+x[0],dp[1][n-1]+x[1]);}staticvoidMain(){int[][]a=newint[][]{newint[]{4,5,3,2},newint[]{2,10,1,4}};int[][]T=newint[][]{newint[]{0,7,4,5},newint[]{0,9,2,8}};int[]e={10,12};int[]x={18,7};intans=carAssembly(a,T,e,x);Console.WriteLine(ans);}}
JavaScript
// Function to find minimum assembly timefunctioncarAssembly(a,T,e,x){letn=a[0].length;// dp[0][i] -> Minimum time to reach station i on// line 0 dp[1][i] -> Minimum time to reach station// i on line 1letdp=Array.from({length:2},()=>Array(n).fill(0));// Base Case:// Add entry time and first station timedp[0][0]=e[0]+a[0][0];dp[1][0]=e[1]+a[1][0];// Fill DP tablefor(leti=1;i<n;i++){// Stay on same line OR switch from other linedp[0][i]=Math.min(dp[0][i-1]+a[0][i],dp[1][i-1]+T[1][i]+a[0][i]);dp[1][i]=Math.min(dp[1][i-1]+a[1][i],dp[0][i-1]+T[0][i]+a[1][i]);}// Add exit timesreturnMath.min(dp[0][n-1]+x[0],dp[1][n-1]+x[1]);}// Driver Codeleta=[[4,5,3,2],[2,10,1,4]];letT=[[0,7,4,5],[0,9,2,8]];lete=[10,12];letx=[18,7];letans=carAssembly(a,T,e,x);console.log(ans);