Table of Contents

# Find Right Interval – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

You are given an array of `intervals`

, where `intervals[i] = [start`

and each _{i}, end_{i}]`start`

is _{i}**unique**.

The **right interval** for an interval `i`

is an interval `j`

such that `start`

and _{j} >= end_{i}`start`

is _{j}**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`

.

**Example 1:**

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

**Example 2:**

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 start_{0}= 3 is the smallest start that is >= end_{1}= 3. The right interval for [1,2] is [2,3] since start_{1}= 2 is the smallest start that is >= end_{2}= 2.

**Example 3:**

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 start_{2}= 3 is the smallest start that is >= end_{1}= 3.

**Constraints:**

`1 <= intervals.length <= 2 * 10`

^{4}`intervals[i].length == 2`

`-10`

^{6}<= start_{i}<= end_{i}<= 10^{6}- The start point of each interval is
**unique**.

# C++ Find Right Interval LeetCode Solution

```
``````
class Solution {
public:
vector<int> findRightInterval(vector<vector<int>>& intervals) {
vector<int> ans(intervals.size());
map<int, int> m;
for(int i = 0; i < intervals.size(); i++)
m[intervals[i][0]] = i;
for(int i = 0; i < intervals.size(); i++)
ans[i] = m.lower_bound(intervals[i][1])!=end(m)?m.lower_bound(intervals[i][1]) -> second:-1;
return ans;
}
};
```

# Java Find Right Interval LeetCode Solution

```
``````
public class Solution {
public int[] findRightInterval(Interval[] intervals) {
int[] result = new int[intervals.length];
java.util.NavigableMap<Integer, Integer> intervalMap = new TreeMap<>();
for (int i = 0; i < intervals.length; ++i) {
intervalMap.put(intervals[i].start, i);
}
for (int i = 0; i < intervals.length; ++i) {
Map.Entry<Integer, Integer> entry = intervalMap.ceilingEntry(intervals[i].end);
result[i] = (entry != null) ? entry.getValue() : -1;
}
return result;
}
}
```

# Python 3 Find Right Interval LeetCode Solution

```
``````
def findRightInterval(self, intervals):
l = sorted((e.start, i) for i, e in enumerate(intervals))
res = []
for e in intervals:
r = bisect.bisect_left(l, (e.end,))
res.append(l[r][1] if r < len(l) else -1)
return res
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1