710088. Assembly Line Scheduling

Medium

Assembly Line Scheduling

Slug: assembly-line-scheduling

Difficulty: Medium

Id: 710088

Topic Tags: None

Company Tags: None

Summary

The Assembly Line Scheduling problem involves scheduling tasks on an assembly line to minimize the total time taken. Given a list of tasks with their respective processing times and dependencies, we need to find the optimal order in which these tasks should be performed to meet the deadlines while minimizing the overall processing time.

Detailed Explanation

Let's break down the problem step by step:

  1. Initialize an empty queue queue and a priority queue pq.
  2. Iterate over each task task in the list of tasks.
  3. For each task, calculate its earliest start time est by iterating from the current time to the deadline and finding the latest point where it can be scheduled without violating any dependencies.
  4. Insert the task into the priority queue with a priority equal to its estimated start time.
  5. Once all tasks are inserted into the priority queue, pop tasks from the queue until it's empty.
  6. For each popped task task, calculate its actual start time ast by considering the earliest available time slot without violating any dependencies.
  7. Update the actual start time of dependent tasks by incrementing their actual start times by the processing time of the current task.
  8. Calculate the total processing time by summing up the processing times of all tasks.

Time Complexity: O(n log n), where n is the number of tasks.

Space Complexity: O(n), where n is the number of tasks.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.*;

public class AssemblyLineScheduling {
    public static int minProcessingTime(int[] tasks, int[][] dependencies) {
        // Initialize queue and priority queue
        Queue<Integer> queue = new LinkedList<>();
        PriorityQueue<Job> pq = new PriorityQueue<>((a, b) -> a.estimatedStartTime - b.estimatedStartTime);

        for (int task : tasks) {
            Job job = new Job(task);
            for (int dependency : dependencies) {
                if (job.id == dependency[0]) {
                    job.dependencies.add(dependency[1]);
                }
            }
            pq.offer(job);
        }

        int totalProcessingTime = 0;
        while (!pq.isEmpty()) {
            Job task = pq.poll();
            int actualStartTime = Math.max(0, task.estimatedStartTime - task.processingTime);
            totalProcessingTime += task.processingTime;

            for (int dependentTask : task.dependencies) {
                pq.peek().actualStartTime = Math.max(pq.peek().actualStartTime, actualStartTime + task.processingTime);
            }
        }

        return totalProcessingTime;
    }

    static class Job implements Comparable<Job> {
        int id;
        int processingTime;
        List<Integer> dependencies;
        int estimatedStartTime;
        int actualStartTime;

        Job(int id) {
            this.id = id;
            this.processingTime = id;
            this.dependencies = new ArrayList<>();
            this.estimatedStartTime = 0;
            this.actualStartTime = 0;
        }

        @Override
        public int compareTo(Job other) {
            return Integer.compare(this.estimatedStartTime, other.estimatedStartTime);
        }
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.