You are given an array of intervals, where intervals[i] = [starti, endi] and each starti is unique.
The right interval for an interval i is an interval j such that startj >= endi and startj is minimized. Note that i may equal j.
Return an array of right interval indices for each interval i. If no right interval exists for interval i, then put -1 at index i.
Example 1:
Input: intervals = [[1,2]] Output: [-1] Explanation: There is only one interval in the collection, so it outputs -1.
Example 2:
Input: intervals = [[3,4],[2,3],[1,2]] Output: [-1,0,1] Explanation: There is no right interval for [3,4]. The right interval for [2,3] is [3,4] since start0 = 3 is the smallest start that is >= end1 = 3. The right interval for [1,2] is [2,3] since start1 = 2 is the smallest start that is >= end2 = 2.
Example 3:
Input: intervals = [[1,4],[2,3],[3,4]] Output: [-1,2,-1] Explanation: There is no right interval for [1,4] and [3,4]. The right interval for [2,3] is [3,4] since start2 = 3 is the smallest start that is >= end1 = 3.
Constraints:
1 <= intervals.length <= 2 * 104intervals[i].length == 2-106 <= starti <= endi <= 106When 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:
The brute force approach to finding the 'right interval' is like checking every possible pairing of intervals to see if one interval starts after another. It's about exhaustively comparing each interval with every other interval. If a 'right interval' can't be found for a particular interval, we'll just mark that we didn't find one.
Here's how the algorithm would work step-by-step:
def find_right_interval_brute_force(intervals):
number_of_intervals = len(intervals)
result = [-1] * number_of_intervals
for interval_index in range(number_of_intervals):
current_interval = intervals[interval_index]
current_interval_end = current_interval[1]
minimum_start = float('inf')
right_interval_index = -1
for other_interval_index in range(number_of_intervals):
if interval_index == other_interval_index:
continue
other_interval = intervals[other_interval_index]
other_interval_start = other_interval[0]
# Check if it's a 'right interval'
if other_interval_start >= current_interval_end:
# Need to find the minimum start value
if other_interval_start < minimum_start:
minimum_start = other_interval_start
right_interval_index = other_interval_index
result[interval_index] = right_interval_index
return resultThe goal is to find, for each interval, another interval whose start is the smallest possible value that is greater than or equal to the end of the original interval. We can achieve this efficiently by sorting all the interval start points and then using a clever searching strategy to quickly locate the desired right interval.
Here's how the algorithm would work step-by-step:
def find_right_interval(intervals):
number_of_intervals = len(intervals)
right_intervals = [-1] * number_of_intervals
start_points_with_index = []
for index, interval in enumerate(intervals):
start_points_with_index.append((interval[0], index))
# Sort start points to enable efficient searching
start_points_with_index.sort()
for index, interval in enumerate(intervals):
end_point = interval[1]
right_interval_index = -1
# Find the smallest start point >= end_point
left_index = 0
right_index = number_of_intervals - 1
while left_index <= right_index:
middle_index = (left_index + right_index) // 2
if start_points_with_index[middle_index][0] >= end_point:
right_interval_index = start_points_with_index[middle_index][1]
right_index = middle_index - 1
else:
left_index = middle_index + 1
right_intervals[index] = right_interval_index
return right_intervals| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty array immediately as there are no intervals to process. |
| Input array with a single interval | Return an array containing -1 since a single interval cannot have a 'right' interval. |
| Intervals with identical start values | The algorithm should correctly identify the right interval based on the specified comparison (start value greater than or equal) after sorting based on start values. |
| No right interval exists for a given interval | The solution should return -1 for intervals that do not have a right interval. |
| Input array with intervals having very large start/end values (potential integer overflow) | Use long or appropriate data types to prevent integer overflow during comparisons and calculations if necessary. |
| Input array where all intervals are overlapping | Each interval should be assigned -1 in the result array, since no interval has a right interval. |
| A right interval exists, but its start value is extremely close to the end value of the original interval (floating point precision issues if using floats) | Ensure the comparison is robust against floating-point inaccuracies by using appropriate tolerance or integer-based representations. |
| Maximum number of intervals allowed by the problem constraint | Ensure the solution doesn't exceed time or space complexity limits for the maximum allowed input size, potentially causing timeouts or out-of-memory errors. |