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:
- Initialize an empty queue
queueand a priority queuepq. - Iterate over each task
taskin the list of tasks. - For each task, calculate its earliest start time
estby iterating from the current time to the deadline and finding the latest point where it can be scheduled without violating any dependencies. - Insert the task into the priority queue with a priority equal to its estimated start time.
- Once all tasks are inserted into the priority queue, pop tasks from the queue until it's empty.
- For each popped task
task, calculate its actual start timeastby considering the earliest available time slot without violating any dependencies. - Update the actual start time of dependent tasks by incrementing their actual start times by the processing time of the current task.
- 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
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);
}
}
}