🧠
Sweep Line Algorithm Using Events
💡 This approach models arrivals and departures as events on a timeline, processing them in order to track platform usage dynamically, which is a powerful technique for interval problems. It generalizes the two-pointer method by treating all events uniformly.
Intuition
Create a combined list of arrival and departure events, sort by time, and sweep through them. Increment platform count on arrival, decrement on departure, tracking the maximum simultaneously needed.
Algorithm
- Create an events list with tuples (time, type), where type is +1 for arrival and -1 for departure.
- Sort the events by time; if times are equal, departures (-1) come before arrivals (+1) to free platforms first.
- Initialize platforms_needed and max_platforms to 0.
- Traverse the events, adding type to platforms_needed at each event.
- Update max_platforms with the maximum platforms_needed during traversal.
- Return max_platforms.
💡 Sorting events and processing them in order simulates the timeline precisely, handling simultaneous arrivals and departures correctly.
def min_platforms_sweep_line(arrivals, departures):
events = []
for time in arrivals:
events.append((time, 1)) # arrival
for time in departures:
events.append((time, -1)) # departure
# Sort by time; if tie, departure (-1) before arrival (1)
events.sort(key=lambda x: (x[0], x[1]))
platforms_needed = max_platforms = 0
for _, event_type in events:
platforms_needed += event_type
max_platforms = max(max_platforms, platforms_needed)
return max_platforms
# Driver code
if __name__ == '__main__':
arrivals = [900, 940, 950, 1100, 1500, 1800]
departures = [910, 1200, 1120, 1130, 1900, 2000]
print(min_platforms_sweep_line(arrivals, departures)) # Output: 3
Line Notes
events = []Initialize list to hold all arrival and departure events.
for time in arrivals:Add all arrival events with +1 indicating platform needed.
for time in departures:Add all departure events with -1 indicating platform freed.
events.sort(key=lambda x: (x[0], x[1]))Sort events by time; departures before arrivals if times are equal to free platforms first.
platforms_needed += event_typeUpdate platform count based on event type.
max_platforms = max(max_platforms, platforms_needed)Track maximum platforms needed.
import java.util.*;
public class MinPlatformsSweepLine {
public static int minPlatforms(int[] arrivals, int[] departures) {
List<int[]> events = new ArrayList<>();
for (int time : arrivals) {
events.add(new int[]{time, 1}); // arrival
}
for (int time : departures) {
events.add(new int[]{time, -1}); // departure
}
events.sort((a, b) -> {
if (a[0] != b[0]) return a[0] - b[0];
return a[1] - b[1]; // departure (-1) before arrival (1)
});
int platformsNeeded = 0, maxPlatforms = 0;
for (int[] event : events) {
platformsNeeded += event[1];
maxPlatforms = Math.max(maxPlatforms, platformsNeeded);
}
return maxPlatforms;
}
public static void main(String[] args) {
int[] arrivals = {900, 940, 950, 1100, 1500, 1800};
int[] departures = {910, 1200, 1120, 1130, 1900, 2000};
System.out.println(minPlatforms(arrivals, departures)); // Output: 3
}
}
Line Notes
List<int[]> events = new ArrayList<>();Create list to hold all events with time and type.
events.add(new int[]{time, 1});Add arrival events with +1 to indicate platform needed.
events.add(new int[]{time, -1});Add departure events with -1 to indicate platform freed.
events.sort((a, b) -> { ... });Sort events by time, departures before arrivals if tie to free platforms first.
platformsNeeded += event[1];Update platform count based on event type.
maxPlatforms = Math.max(maxPlatforms, platformsNeeded);Track maximum platforms needed.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int minPlatformsSweepLine(const vector<int>& arrivals, const vector<int>& departures) {
vector<pair<int, int>> events;
for (int time : arrivals) {
events.emplace_back(time, 1); // arrival
}
for (int time : departures) {
events.emplace_back(time, -1); // departure
}
sort(events.begin(), events.end(), [](const pair<int,int>& a, const pair<int,int>& b) {
if (a.first != b.first) return a.first < b.first;
return a.second < b.second; // departure (-1) before arrival (1)
});
int platformsNeeded = 0, maxPlatforms = 0;
for (auto& event : events) {
platformsNeeded += event.second;
maxPlatforms = max(maxPlatforms, platformsNeeded);
}
return maxPlatforms;
}
int main() {
vector<int> arrivals = {900, 940, 950, 1100, 1500, 1800};
vector<int> departures = {910, 1200, 1120, 1130, 1900, 2000};
cout << minPlatformsSweepLine(arrivals, departures) << endl; // Output: 3
return 0;
}
Line Notes
vector<pair<int, int>> events;Create vector to hold all events with time and type.
events.emplace_back(time, 1);Add arrival events with +1 indicating platform needed.
events.emplace_back(time, -1);Add departure events with -1 indicating platform freed.
sort(events.begin(), events.end(), ...);Sort events by time; departures before arrivals if tie.
platformsNeeded += event.second;Update platform count based on event type.
maxPlatforms = max(maxPlatforms, platformsNeeded);Track maximum platforms needed.
function minPlatformsSweepLine(arrivals, departures) {
const events = [];
for (const time of arrivals) {
events.push([time, 1]); // arrival
}
for (const time of departures) {
events.push([time, -1]); // departure
}
events.sort((a, b) => {
if (a[0] !== b[0]) return a[0] - b[0];
return a[1] - b[1]; // departure (-1) before arrival (1)
});
let platformsNeeded = 0, maxPlatforms = 0;
for (const [, type] of events) {
platformsNeeded += type;
maxPlatforms = Math.max(maxPlatforms, platformsNeeded);
}
return maxPlatforms;
}
// Test
const arrivals = [900, 940, 950, 1100, 1500, 1800];
const departures = [910, 1200, 1120, 1130, 1900, 2000];
console.log(minPlatformsSweepLine(arrivals, departures)); // Output: 3
Line Notes
const events = [];Initialize array to hold all events with time and type.
events.push([time, 1]);Add arrival events with +1 indicating platform needed.
events.push([time, -1]);Add departure events with -1 indicating platform freed.
events.sort((a, b) => { ... });Sort events by time; departures before arrivals if tie.
platformsNeeded += type;Update platform count based on event type.
maxPlatforms = Math.max(maxPlatforms, platformsNeeded);Track maximum platforms needed.
Sorting events takes O(n log n), and processing them is O(n).
💡 This approach uses extra space for events but is efficient and clear for handling simultaneous events.
Interview Verdict: Accepted
This is an optimal and elegant approach, especially useful when event attributes grow more complex.