Interval & Sweepline
public int[][] merge(int[][] intervals) {
if (intervals.length == 0) return new int[][]{};
List<int[]> list = new ArrayList<>();
Arrays.sort(intervals, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
// if (a[0] == b[0]) return a[1] - b[1];
return a[0] - b[0];
}
});
int start = intervals[0][0], end = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
int[] in = intervals[i];
if (in[0] > end) {
list.add(new int[]{start, end});
start = in[0];
end = in[1];
} else {
end = Math.max(end, in[1]);
}
}
list.add(new int[]{start, end});
int[][] res = new int[list.size()][2];
for (int i = 0; i < list.size(); i++) res[i] = list.get(i);
return res;
}Use capacity to keep track of the capacity on the vehicle. The capacity is reduced when adding passengers, and the capacity is increased when release passengers. When capacity < 0, return false.
Sort the trips by start location, and loop over the trips. Use a PriorityQueue and rank by end location to extract the earliest ended trips. Release capacity of those ended trips, and check valid.
Sort and determine it the prev and curt interval has overlap.
Sort the intervals by start time, use Heap(PQ) to sort by end time. Loop over all intervals, detect if the latest ended meeting in heap has overlap between the current meeting.
if yes, add curt to heap
otherwise extend the previous meeting in heap.
TreeMap:
Use two lists to store intervals, if there is an existing overlap, then add the overlap portion to the next list. When adding a new interval, check if there is a overlap with the second list.
TreeMap sweepline
Sort the balloons by end coordinate
x_end.Initiate the end coordinate of a balloon which ends first:
first_end = points[0][1].Initiate the number of arrows:
arrows = 1.Iterate over all balloons:
If the balloon starts after
first_end:Increase the number of arrows by one.
Set
first_endto be equal to the end of the current balloon.
Return arrows.
Sort the slots by starting time. Use two pointers and calculate the overlap between two slots, if overlap >= duration, return the result. Move the pointer with earlier starting time.
Store all number ranges in a TreeMap and detect if there is an overlap or extension.
Store all ranges in a TreeMap and detect the ones that overlap, starts with the floorKey then higherKey.
3355. Zero Array Transformation I
1288. Remove Covered Intervals
sort by starting time, if starting time equals, the interval end last ranks to the front
if next end <= prev end, means overlap, count the number of overlaps
update prevEnd if no overlap
3439. Reschedule Meetings for Maximum Free Time I
You're given the total duration of an event and multiple smaller events with their respective start and end times. The goal is to determine the maximum total free time that can be obtained by selecting k + 1 non-overlapping gaps between the events.
Approach
First, compute the gaps between the events:
Gap before the first event:
startTime[0]Gaps between events:
startTime[i] - endTime[i - 1]Gap after the last event:
eventTime - endTime[n - 1]
We store these gaps in a vector
gapof sizen + 1.Then, apply a sliding window of size
k + 1to find the maximum sum of anyk + 1contiguous gaps.
LeetCode Solution: Maximum Free Time by Event Manipulation
Problem Overview Given:
eventTime: Total available time durationstartTime[]andendTime[]: Arrays defining scheduled events
Objective: Maximize continuous free time by strategically moving or removing one event to merge adjacent time gaps.
Visual Demo
Example: eventTime = 12, Events: [1,3], [4,6], [9,11]
eventTime = 12, Events: [1,3], [4,6], [9,11]Initial Timeline:
Gaps: [1, 1, 3, 1] (before evt1, between evt1-evt2, between evt2-evt3, after evt3)
Strategy 1: Move Event
Move Event 2 (duration=2) to fit in Gap 3 (size=3):
Result: Combined free time = 3 units
Strategy 2: Remove Event
Remove Event 2 entirely:
Result: Combined free time = 6 units
Algorithm Breakdown
1. Gap Calculation
2. Right-Side Maximum Tracking
3. Event Processing
For each event i:
Can move? Check if
event_duration ≤ max(maxL, maxR[i])Move strategy:
result = gap[i-1] + duration + gap[i]Remove strategy:
result = gap[i-1] + gap[i]
4. Example Walkthrough
Maximum Result: 4 units of continuous free time
Key Insights
Event relocation works only when destination gap ≥ event duration
Event removal always merges adjacent gaps
Optimal strategy depends on gap sizes and event durations
Greedy approach considers both options for each event position
Time Complexity: O(n) | Space Complexity: O(n)
Last updated
Was this helpful?