Assembly Line SchedulingProblem

Pattern: 2D grid DP

medium

A car passes through n stations on one of two lines. Staying on a line costs only the station time; switching costs a transfer time. Track the best time to finish station j on each line: stay on the same line, or come across from the other.

The problem

Each line i has station times a[i][j]. Entering line i costs e[i] and leaving it costs x[i]. Moving from line i after station j - 1 to the other line's station j costs t[i][j] (t[i][0] is unused). Return the least time to build a car through all n stations.

Think first

  • What two numbers describe progress after station j?
  • How can you arrive at line 0's station j?
  • What is added at the end?

Hints

    Brute force, and why it fails

    Try every choice of line at every station: 2ⁿ.

    The key insight

    Only which line you are on matters, so two numbers per station are enough.

    Dry run

    line014
    line114

    Line 0 times 4 5 3 2, line 1 times 2 10 1 4; entry 10 and 12. Station 0: 14 and 14.

    Step 1 of 3

    The plan

    l0 = e0 + a0[0]; l1 = e1 + a1[0]
    for j: l0, l1 = min(l0, l1 + t1[j]) + a0[j], min(l1, l0 + t0[j]) + a1[j]
    return min(l0 + x0, l1 + x1)
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    Two numbers per station.

    Edge cases

    • One station.
    • Free transfers.
    • Huge transfers: never switch.
    I've built my logic, show the code

    Java

    class Solution {
        public int carAssembly(int[][] a, int[][] t, int[] e, int[] x) {
            int n = a[0].length;
            int line0 = e[0] + a[0][0], line1 = e[1] + a[1][0];   // the best time to finish station j on each line
            for (int j = 1; j < n; j++) {
                int next0 = Math.min(line0, line1 + t[1][j]) + a[0][j];   // stay on line 0, or come across from line 1
                int next1 = Math.min(line1, line0 + t[0][j]) + a[1][j];
                line0 = next0;
                line1 = next1;
            }
            return Math.min(line0 + x[0], line1 + x[1]);
        }
    }

    C++

    class Solution {
    public:
        int carAssembly(vector<vector<int>>& a, vector<vector<int>>& t, vector<int>& e, vector<int>& x) {
            int n = static_cast<int>(a[0].size());
            int line0 = e[0] + a[0][0], line1 = e[1] + a[1][0];   // the best time to finish station j on each line
            for (int j = 1; j < n; j++) {
                int next0 = min(line0, line1 + t[1][j]) + a[0][j];    // stay on line 0, or come across from line 1
                int next1 = min(line1, line0 + t[0][j]) + a[1][j];
                line0 = next0;
                line1 = next1;
            }
            return min(line0 + x[0], line1 + x[1]);
        }
    };

    Python

    class Solution:
        def carAssembly(self, a: list[list[int]], t: list[list[int]], e: list[int], x: list[int]) -> int:
            line0, line1 = e[0] + a[0][0], e[1] + a[1][0]   # the best time to finish station j on each line
            for j in range(1, len(a[0])):
                line0, line1 = (min(line0, line1 + t[1][j]) + a[0][j],   # stay, or come across from the other line
                                min(line1, line0 + t[0][j]) + a[1][j])
            return min(line0 + x[0], line1 + x[1])

    Common mistakes

    • Using the wrong line's transfer cost.
    • Forgetting the entry or exit times.

    Similar problems