Find Right Interval

Try to solve the Find Right Interval problem.

Statement

You are given an array of intervals where each interval is represented by a pair [starti,endi][start_i, end_i]. The startistart_i values are unique, meaning no two intervals begin at the same time.

The task is to find the right interval for each interval in the list. The right interval for an interval ii is an interval jj such that startj>=endistart_j >= end_i and startjstart_j is minimized (i.e., it is the smallest start time among all valid intervals that is greater than or equal to endiend_i). Note that ii may equal jj.

Return an array of right interval indexes for each interval ii. If no right interval exists for interval ii, then put 1-1 at index ii.

Constraints:

  • 11 \leq intervals.length 1000\leq 1000

  • intervals[i].length ==2== 2

  • 106startiendi106-10^6 \leq start_i \leq end_i \leq 10^6

  • The start times are guaranteed to be unique.

Examples

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.