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,-1]
💡 Note:
For [1,4]: no start >= 4, return -1. For [2,3]: [3,4] starts at 3 >= 3, return index 2. For [3,4]: no start >= 4, return -1
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code