Merge intervals Flashcards

(5 cards)

1
Q

What is the definition of Merge Intervals?

A

Merges overlapping intervals in a sorted list.

Use Case: Scheduling, overlapping ranges.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the key steps for Merge Intervals?

A
  1. Sort intervals by start time.
  2. Iterate, merge if current start ≤ previous end.
  3. Add non-overlapping intervals to result.

Action: Explain steps for Merge Intervals aloud.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the problem and approach for Merge Intervals?

A

Problem: Merge overlapping intervals.
Approach: Sort by start, merge if overlapping, keep non-overlapping.

Action: Verbalize solution logic aloud.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the complexity and gotchas of Merge Intervals?

A

Complexity: Time: O(n log n), Space: O(n).
Gotchas: Empty list, no overlaps.

Action: List edge cases for Merge Intervals aloud.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Provide a visual or code example for Merge Intervals.

A

```python
from typing import List
def merge(intervals: List[List[int]]) -> List[List[int]]:
if not intervals:
return []
intervals.sort(key=lambda x: x[0])
result = [intervals[0]]
for start, end in intervals[1:]:
if start <= result[-1][1]:
result[-1][1] = max(result[-1][1], end)
else:
result.append([start, end])
return result
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly