VOOZH about

URL: https://www.geeksforgeeks.org/dsa/assembly-line-scheduling-dp-34/

⇱ Assembly Line Scheduling - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Assembly Line Scheduling

Last Updated : 24 May, 2026

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 to assembly 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.
👁 2056958061

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.

👁 2056958060

[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.

Output
35

Consider the following example: line = 2, stations = 3

👁 s111
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.

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.

Output
35
Comment
Article Tags: