Find Right Interval - Problem
You are given an array of intervals, where intervals[i] = [start_i, end_i] and each start_i is unique.
The right interval for an interval i is an interval j such that start_j >= end_i and start_j 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.
Input & Output
Example 1 — Basic Case
$
Input:
intervals = [[1,2]]
›
Output:
[-1]
💡 Note:
There is only one interval, no right interval exists for [1,2], so return -1
Example 2 — Multiple Intervals
$
Input:
intervals = [[3,4],[2,3],[1,2]]
›
Output:
[-1,0,1]
💡 Note:
For [3,4]: no interval starts >= 4, return -1. For [2,3]: [3,4] starts at 3 >= 3, return index 0. For [1,2]: [2,3] starts at 2 >= 2, return index 1
Example 3 — Self Reference
$
Input:
intervals = [[1,4],[2,3],[3,4]]
›
Output:
[-1,2,2]
💡 Note:
For [1,4]: no start >= 4, return -1. For [2,3]: [3,4] starts at 3 >= 3, return index 2. For [3,4]: itself starts at 3 >= 4 is false, but no other valid interval, return 2 (self)
Constraints
- 1 ≤ intervals.length ≤ 2 × 104
- intervals[i].length == 2
- -106 ≤ starti ≤ endi ≤ 106
- The start point of each interval is unique.
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of intervals with unique start times
2
Process
For each interval, find the interval with smallest start ≥ end
3
Output
Array of right interval indices or -1 if none exists
Key Takeaway
🎯 Key Insight: Sort intervals by start time to enable efficient binary search for the right interval
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code