Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Find Right Interval

XPChallenge Points: 20
levelLevel: Medium

You are given a list of intervals, where each interval contains a start and end value. Every start value is unique.

For each interval, we want to find another interval whose start value is greater than or equal to the end value of the current interval, and among all such intervals, choose the one with the smallest start value.

If such an interval exists, we return its index.
If not, we return -1 for that interval.

Finally, return the result as an array.

Example 1:

Input: intervals = [[2,5]]

Output: [-1]

Explanation:

No interval starts at or after 5.

Example 2:

Input: intervals = [[1,3],[4,6],[7,8]]

Output: [1,2,-1]

Explanation:

For [1,3], next interval is [4,6] → index 1 For [4,6], next interval is [7,8] → index 2 For [7,8], no next interval → -1

Example 3:

Input: intervals = [[5,7],[1,2],[3,5]]

Output: [ -1, 2, 0 ]

Explanation:

For [1,2], next start >= 2 is 3 → interval index 2. For [3,5], next start >= 5 is 5 → interval index 0. For [5,7], no interval starts ≥ 7 → -1.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result