> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find Right Interval Python Solution with Tests

> Tested Python solution for LeetCode 436 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 436, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/find-right-interval/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 436   # by problem number
lcpy gen -s find_right_interval   # by problem name
```

## Problem

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.

### Examples

```
Input: intervals = [[1,2]]
Output: [-1]
Explanation: There is only one interval in the collection, so it outputs -1.
```

```
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.
```

```
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 \* 10^4
intervals\[i].length == 2
-10^6 \<= starti \<= endi \<= 10^6
The start point of each interval is unique.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_right_interval/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_right_interval/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import bisect


class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def find_right_interval(self, intervals: list[list[int]]) -> list[int]:
        sorted_starts = sorted((interval[0], i) for i, interval in enumerate(intervals))
        starts = [start for start, _ in sorted_starts]
        result: list[int] = []
        for interval in intervals:
            pos = bisect.bisect_left(starts, interval[1])
            result.append(sorted_starts[pos][1] if pos < len(starts) else -1)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.