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 alln stations inorder, 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.
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;intsolve(vector<vector<int>>&a,vector<vector<int>>&t,intcl,intcs,vector<int>&x,intn){// Base Case:// If we are at the last station,// add exit time of the current lineif(cs==n-1){returnx[cl];}// Option 1:// Continue on the same assembly lineintsame=solve(a,t,cl,cs+1,x,n)+a[cl][cs+1];// Option 2:// Switch to the other assembly lineintchange=solve(a,t,!cl,cs+1,x,n)+a[!cl][cs+1]+t[cl][cs+1];// Return minimum of both choicesreturnmin(same,change);}// 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.*;publicclassGFG{staticintsolve(int[][]a,int[][]t,intcl,intcs,int[]x,intn){// Base Case:// If we are at the last stationif(cs==n-1){returnx[cl];}// Option 1:// Continue on same lineintsame=solve(a,t,cl,cs+1,x,n)+a[cl][cs+1];// Option 2:// Switch to another lineintchange=solve(a,t,1-cl,cs+1,x,n)+a[1-cl][cs+1]+t[cl][cs+1];// Return minimumreturnMath.min(same,change);}// Function to find minimum assembly timepublicstaticintcarAssembly(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);}publicstaticvoidmain(String[]args){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(a,t,e,x);System.out.println(ans);}}
Python
defsolve(a,t,cl,cs,x,n):# Base Case:# If we are at the last stationifcs==n-1:returnx[cl]# Option 1:# Continue on same linesame=solve(a,t,cl,cs+1,x,n)+a[cl][cs+1]# Option 2:# Switch to another linechange=solve(a,t,1-cl,cs+1,x,n)+ \
a[1-cl][cs+1]+t[cl][cs+1]# Return minimumreturnmin(same,change)# Function to find minimum assembly timedefcarAssembly(a,t,e,x):n=len(a[0])# 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__":a=[[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(a,t,e,x)print(ans)
C#
usingSystem;classGFG{staticintSolve(int[][]a,int[][]t,intcl,intcs,int[]x,intn){// Base Case:// If we are at the last stationif(cs==n-1){returnx[cl];}// Option 1:// Continue on same lineintsame=Solve(a,t,cl,cs+1,x,n)+a[cl][cs+1];// Option 2:// Switch to another lineintchange=Solve(a,t,1-cl,cs+1,x,n)+a[1-cl][cs+1]+t[cl][cs+1];// Return minimumreturnMath.Min(same,change);}// 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
functionsolve(a,t,cl,cs,x,n){// Base Case:// If we are at the last stationif(cs===n-1){returnx[cl];}// Option 1:// Continue on same lineletsame=solve(a,t,cl,cs+1,x,n)+a[cl][cs+1];// Option 2:// Switch to another lineletchange=solve(a,t,1-cl,cs+1,x,n)+a[1-cl][cs+1]+t[cl][cs+1];// Return minimumreturnMath.min(same,change);}// 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.
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,intcl,intcs,vector<int>&x,intn,vector<vector<int>>&dp){// Base Case:// If we are at the last station,// add exit time of current lineif(cs==n-1){returnx[cl];}// If already computedif(dp[cl][cs]!=-1){returndp[cl][cs];}// Option 1:// Continue on same assembly lineintsame=solve(a,t,cl,cs+1,x,n,dp)+a[cl][cs+1];// Option 2:// Switch to the other assembly lineintchange=solve(a,t,!cl,cs+1,x,n,dp)+a[!cl][cs+1]+t[cl][cs+1];// Store and return minimum answerreturndp[cl][cs]=min(same,change);}// 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.*;publicclassGFG{// Recursive + Memoization functionstaticintsolve(int[][]a,int[][]t,intcl,intcs,int[]x,intn,int[][]dp){// Base Case:// If we are at the last stationif(cs==n-1){returnx[cl];}// If already computedif(dp[cl][cs]!=-1){returndp[cl][cs];}// Option 1:// Continue on same lineintsame=solve(a,t,cl,cs+1,x,n,dp)+a[cl][cs+1];// Option 2:// Switch to another lineintchange=solve(a,t,1-cl,cs+1,x,n,dp)+a[1-cl][cs+1]+t[cl][cs+1];// Store and return minimum answerreturndp[cl][cs]=Math.min(same,change);}// Function to find minimum assembly timepublicstaticintcarAssembly(int[][]a,int[][]t,int[]e,int[]x){intn=a[0].length;// 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){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(a,t,e,x);System.out.println(ans);}}
Python
# Recursive + Memoization functiondefsolve(a,t,cl,cs,x,n,dp):# Base Case:# If we are at the last stationifcs==n-1:returnx[cl]# If already computedifdp[cl][cs]!=-1:returndp[cl][cs]# Option 1:# Continue on same linesame=solve(a,t,cl,cs+1,x,n,dp)+a[cl][cs+1]# Option 2:# Switch to another linechange=solve(a,t,1-cl,cs+1,x,n,dp)+ \
a[1-cl][cs+1]+ \
t[cl][cs+1]# Store and return minimum answerdp[cl][cs]=min(same,change)returndp[cl][cs]# Function to find minimum assembly timedefcarAssembly(a,t,e,x):n=len(a[0])# 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__":a=[[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(a,t,e,x)print(ans)
C#
usingSystem;classGFG{// Recursive + Memoization functionstaticintSolve(int[][]a,int[][]t,intcl,intcs,int[]x,intn,int[][]dp){// Base Case:// If we are at the last stationif(cs==n-1){returnx[cl];}// If already computedif(dp[cl][cs]!=-1){returndp[cl][cs];}// Option 1:// Continue on same lineintsame=Solve(a,t,cl,cs+1,x,n,dp)+a[cl][cs+1];// Option 2:// Switch to another lineintchange=Solve(a,t,1-cl,cs+1,x,n,dp)+a[1-cl][cs+1]+t[cl][cs+1];// Store and return minimum answerdp[cl][cs]=Math.Min(same,change);returndp[cl][cs];}// 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,cl,cs,x,n,dp){// Base Case:// If we are at the last stationif(cs===n-1){returnx[cl];}// If already computedif(dp[cl][cs]!==-1){returndp[cl][cs];}// Option 1:// Continue on same lineletsame=solve(a,t,cl,cs+1,x,n,dp)+a[cl][cs+1];// Option 2:// Switch to another lineletchange=solve(a,t,1-cl,cs+1,x,n,dp)+a[1-cl][cs+1]+t[cl][cs+1];// Store and return minimum answerdp[cl][cs]=Math.min(same,change);returndp[cl][cs];}// 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.
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.*;publicclassGFG{// Function to find minimum assembly timepublicstaticintcarAssembly(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][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){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(a,t,e,x);System.out.println(ans);}}
Python
# Function to find minimum assembly timedefcarAssembly(a,t,e,x):n=len(a[0])# 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__":a=[[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(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);
Output
35
[Optimized Approach] Using Constant Space - O(n) Time and O(1) Space
The idea is that each state depends only on the previous station's values, not on the entire DP table. Therefore, instead of storing all n states, we can keep only two variables representing the minimum time to reach the current station on both lines and update them iteratively, reducing space complexity from O(n) to O(1).
Initialize first and second with the minimum time to leave the first station on both lines (including entry time).
Traverse all remaining stations from 1 to n-1.
For each station, compute the minimum time for both lines by either staying on the same line or switching from the other line.
Update first and second with the new values.
Add exit times and return the minimum total 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();// Time to leave first station on each lineintfirst=e[0]+a[0][0];intsecond=e[1]+a[1][0];// Process remaining stationsfor(inti=1;i<n;i++){// Stay on same line OR switch from other lineintup=min(first+a[0][i],// stay on line 1second+t[1][i]+a[0][i]// switch from line 2);intdown=min(second+a[1][i],// stay on line 2first+t[0][i]+a[1][i]// switch from line 1);first=up;second=down;}// Add exit timesreturnmin(first+x[0],second+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
classSolution{// Function to find minimum assembly timepublicstaticintcarAssembly(int[][]a,int[][]t,int[]e,int[]x){intn=a[0].length;// Time to leave first station on each lineintfirst=e[0]+a[0][0];intsecond=e[1]+a[1][0];// Process remaining stationsfor(inti=1;i<n;i++){// Stay on same line or switch from other lineintup=Math.min(first+a[0][i],// stay on line 1second+t[1][i]+a[0][i]// switch from line 2);intdown=Math.min(second+a[1][i],// stay on line 2first+t[0][i]+a[1][i]// switch from line 1);first=up;second=down;}// Add exit timesreturnMath.min(first+x[0],second+x[1]);}publicstaticvoidmain(String[]args){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(a,t,e,x);System.out.println(ans);}}
Python
# Function to find minimum assembly timedefcarAssembly(a,t,e,x):n=len(a[0])# Time to leave first station on each linefirst=e[0]+a[0][0]second=e[1]+a[1][0]# Process remaining stationsforiinrange(1,n):# Stay on same line or switch from other lineup=min(first+a[0][i],# stay on line 1second+t[1][i]+a[0][i]# switch from line 2)down=min(second+a[1][i],# stay on line 2first+t[0][i]+a[1][i]# switch from line 1)first=upsecond=down# Add exit timesreturnmin(first+x[0],second+x[1])# Driver Codeif__name__=="__main__":a=[[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(a,t,e,x)print(ans)
C#
usingSystem;classSolution{// Function to find minimum assembly timepublicstaticintcarAssembly(int[][]a,int[][]t,int[]e,int[]x){intn=a[0].Length;// Time to leave first station on each lineintfirst=e[0]+a[0][0];intsecond=e[1]+a[1][0];// Process remaining stationsfor(inti=1;i<n;i++){// Stay on same line or switch from other lineintup=Math.Min(first+a[0][i],// stay on line 1second+t[1][i]+a[0][i]// switch from line 2);intdown=Math.Min(second+a[1][i],// stay on line 2first+t[0][i]+a[1][i]// switch from line 1);first=up;second=down;}// Add exit timesreturnMath.Min(first+x[0],second+x[1]);}staticvoidMain(){int[][]a={newint[]{4,5,3,2},newint[]{2,10,1,4}};int[][]t={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;// Time to leave first station on each lineletfirst=e[0]+a[0][0];letsecond=e[1]+a[1][0];// Process remaining stationsfor(leti=1;i<n;i++){// Stay on same line or switch from other lineletup=Math.min(first+a[0][i],// stay on line 1second+t[1][i]+a[0][i]// switch from line 2);letdown=Math.min(second+a[1][i],// stay on line 2first+t[0][i]+a[1][i]// switch from line 1);first=up;second=down;}// Add exit timesreturnMath.min(first+x[0],second+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);