You are given a 2D integer array intervals where intervals[i] = [starti, endi] represents all the integers from starti to endi inclusively.
A containing set is an array nums where each interval from intervals has at least two integers in nums.
intervals = [[1,3], [3,7], [8,9]], then [1,2,4,7,8,9] and [2,3,4,8,9] are containing sets.Return the minimum possible size of a containing set.
Example 1:
Input: intervals = [[1,3],[3,7],[8,9]] Output: 5 Explanation: let nums = [2, 3, 4, 8, 9]. It can be shown that there cannot be any containing array of size 4.
Example 2:
Input: intervals = [[1,3],[1,4],[2,5],[3,5]] Output: 3 Explanation: let nums = [2, 3, 4]. It can be shown that there cannot be any containing array of size 2.
Example 3:
Input: intervals = [[1,2],[2,3],[2,4],[4,5]] Output: 5 Explanation: let nums = [1, 2, 3, 4, 5]. It can be shown that there cannot be any containing array of size 4.
Constraints:
1 <= intervals.length <= 3000intervals[i].length == 20 <= starti < endi <= 108When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of overlapping intervals and you need to pick the smallest possible set of numbers such that every interval contains at least two of the numbers you chose. The brute force strategy involves testing every conceivable combination of numbers to find the smallest group that satisfies the requirement for every single interval.
Here's how the algorithm would work step-by-step:
def intersection_size_two(intervals):
flat_numbers = []
for interval in intervals:
flat_numbers.append(interval[0])
flat_numbers.append(interval[1])
minimum_value = min(flat_numbers)
maximum_value = max(flat_numbers)
possible_pool = list(range(minimum_value, maximum_value + 1))
successful_candidates = []
# Generate combinations from smallest possible size upward to guarantee minimality
for combination_size in range(1, len(possible_pool) + 1):
import itertools
for combination in itertools.combinations(possible_pool, combination_size):
is_valid_combination = True
for interval in intervals:
match_count = 0
for candidate_number in combination:
if interval[0] <= candidate_number <= interval[1]:
match_count += 1
# If an interval has fewer than two numbers, this combination fails completely
if match_count < 2:
is_valid_combination = False
break
# Save valid combinations that successfully cover every interval with at least two numbers
if is_valid_combination:
successful_candidates.append(combination)
# Stop early once we find valid combinations at the current smallest possible size
if successful_candidates:
return len(successful_candidates[0])
return 0To find the smallest set of numbers that touches every given interval at least twice, we can use a greedy strategy. By organizing the intervals carefully and always picking numbers from the far right edge of where we need them, we satisfy the rules for multiple intervals at the same time.
Here's how the algorithm would work step-by-step:
def intersection_size_two(intervals):
intervals.sort(key=lambda current_interval: (current_interval[1], -current_interval[0]))
# Sorting by end ascending and start descending ensures optimal greedy choices for future intervals.
chosen_numbers = []
for current_interval in intervals:
interval_start = current_interval[0]
interval_end = current_interval[1]
overlap_count = sum(1 for chosen in chosen_numbers if interval_start <= chosen <= interval_end)
# Picking numbers from the far right edge maximizes overlap utility with subsequent intervals.
if overlap_count == 0:
chosen_numbers.append(interval_end - 1)
chosen_numbers.append(interval_end)
elif overlap_count == 1:
if not (interval_start <= chosen_numbers[-1] <= interval_end):
chosen_numbers.append(interval_end)
else:
chosen_numbers.append(interval_start)
chosen_numbers.sort()
# Maintaining the sorted order of chosen numbers allows precise overlap counting in subsequent iterations.
return len(chosen_numbers)| Case | How to Handle |
|---|---|
| Input array with a single interval | The greedy algorithm will simply pick the two largest numbers within that single interval and return two. |
| Intervals with negative numbers and zero | Sorting intervals by their end points in ascending order naturally accommodates negative numbers, zeros, and positive integers without shifting logic. |
| Intervals with identical start points but different end points | Sorting primarily by end points ascending and secondarily by start points descending ensures we process inner or smaller intervals first. |
| Nested intervals where one interval is completely inside another | Processing sorted intervals guarantees smaller inner intervals are satisfied first, so points chosen for them will also contribute to outer intervals. |
| Intervals with a length of zero where start equals end | An interval where start equals end can only provide one point, requiring adjacent intervals or careful point selection to satisfy the two-point requirement. |
| Massive coordinate values approaching integer limits | Standard 32-bit or 64-bit integer types prevent overflow during comparisons since we only evaluate interval boundaries directly. |
| Maximum constraint size with thousands of overlapping intervals | An O(N log N) sorting-based greedy approach ensures the solution executes well within standard interview time limits. |
| Disjoint intervals with large gaps between them | The greedy tracker will reset its two-point set independently for each disjoint group, adding two new points for every unintersected gap. |