Skip to content

Commit fa64b9b

Browse files
committed
Added Solutions to Leetcode-436
1 parent cd84cc2 commit fa64b9b

File tree

2 files changed

+227
-1
lines changed

2 files changed

+227
-1
lines changed

dsa-problems/leetcode-problems/0400-0499.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export const problems = [
230230
"problemName": "436. Find Right Interval",
231231
"difficulty": "Medium",
232232
"leetCodeLink": "https://leetcode.com/problems/find-right-interval",
233-
"solutionLink": "#"
233+
"solutionLink": "/dsa-solutions/lc-solutions/0400-0499/find-right-interval"
234234
},
235235
{
236236
"problemName": "437. Path Sum III",
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
---
2+
id: find-right-interval
3+
title: Find Right Interval
4+
sidebar_label: 0436 - Find Right Interval
5+
tags:
6+
- Binary Search
7+
- Binary Tree
8+
- Sorting
9+
description: "This is a solution to the Find Right Interval problem on LeetCode."
10+
---
11+
12+
## Problem Description
13+
14+
You are given an array of intervals, where intervals[i] = [$\text{start}_i$, $\text{end}_i$] and each $\text{start}_i$ is **unique**.
15+
16+
The **right interval** for an interval i is an interval j such that $\text{start}_j$ >= $\text{end}_j$ and $\text{start}_j$ is **minimized**. Note that i may equal j.
17+
18+
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.
19+
20+
### Examples
21+
22+
**Example 1:**
23+
24+
```
25+
Input: intervals = [[1,2]]
26+
Output: [-1]
27+
Explanation: There is only one interval in the collection, so it outputs -1.
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Input: intervals = [[1,4],[2,3],[3,4]]
34+
Output: [-1,2,-1]
35+
Explanation: There is no right interval for [1,4] and [3,4].
36+
The right interval for [2,3] is [3,4] since start2 = 3 is the smallest start that is >= end1 = 3.
37+
```
38+
39+
### Constraints
40+
41+
- `1 <= intervals.length <= 2 * 104`
42+
- `intervals[i].length == 2`
43+
- $-10^6 \leq \text{start}_i \leq \text{end}_i \leq 10^6$
44+
- The start point of each interval is **unique**.
45+
46+
## Solution for Find Right Interval
47+
48+
### Intuition
49+
50+
The problem at hand requires finding the "right interval" for each interval in a list. A right interval for an interval i is defined as the smallest interval j such that the start of j is greater than or equal to the end of i. If no such interval exists, the result should be -1.
51+
52+
### Approach
53+
54+
1. **Data Transformation:** Start by transforming the given intervals into a new array where each element keeps track of the original start and end points as well as the original index. This helps in maintaining the reference to the original indices after sorting.
55+
56+
2. **Sorting:** Sort the transformed array based on the start points. Sorting helps in efficiently searching for the right interval using binary search.
57+
58+
3. **Binary Search:** For each interval in the sorted array, use binary search to find the smallest interval whose start is greater than or equal to the end of the current interval. The binary search operates over the sorted start points.
59+
60+
4. **Mapping Results:** The result of the binary search for each interval gives the index of the right interval in the sorted array. This index is then mapped back to the original index using the stored original index in the transformed array.
61+
62+
5. **Output Formation:** Construct the output array where each position corresponds to the original interval's right interval index.
63+
64+
## Code in Different Languages
65+
66+
<Tabs>
67+
<TabItem value="cpp" label="C++">
68+
<SolutionAuthor name="@Shreyash3087"/>
69+
70+
```cpp
71+
#include <vector>
72+
#include <algorithm>
73+
74+
class Solution {
75+
public:
76+
std::vector<int> findRightInterval(std::vector<std::vector<int>>& intv) {
77+
int n = intv.size();
78+
std::vector<std::vector<int>> arr(n, std::vector<int>(3));
79+
80+
for (int i = 0; i < n; ++i) {
81+
arr[i][0] = intv[i][0];
82+
arr[i][1] = intv[i][1];
83+
arr[i][2] = i;
84+
}
85+
86+
std::sort(arr.begin(), arr.end(), [](const std::vector<int>& a, const std::vector<int>& b) {
87+
return a[0] < b[0];
88+
});
89+
90+
std::vector<int> ans(n);
91+
for (int i = 0; i < n; ++i) {
92+
ans[arr[i][2]] = binarySearch(arr, i, n - 1, arr[i][1]);
93+
}
94+
95+
return ans;
96+
}
97+
98+
private:
99+
int binarySearch(const std::vector<std::vector<int>>& arr, int low, int high, int target) {
100+
int result = -1;
101+
102+
while (low <= high) {
103+
int mid = low + (high - low) / 2;
104+
105+
if (arr[mid][0] >= target) {
106+
result = arr[mid][2];
107+
high = mid - 1;
108+
} else {
109+
low = mid + 1;
110+
}
111+
}
112+
return result;
113+
}
114+
};
115+
116+
```
117+
</TabItem>
118+
<TabItem value="java" label="Java">
119+
<SolutionAuthor name="@Shreyash3087"/>
120+
121+
```java
122+
class Solution {
123+
public int[] findRightInterval(int[][] intv) {
124+
int n=intv.length;
125+
int [][] arr=new int[n][3];
126+
127+
for(int i=0;i<n;i++){
128+
129+
arr[i][0]=intv[i][0];
130+
arr[i][1]=intv[i][1];
131+
arr[i][2]=i;
132+
}
133+
134+
135+
Arrays.sort(arr,(a,b)->a[0]-b[0]);
136+
137+
int ans[]=new int[n];
138+
for(int i=0;i<n;i++){
139+
140+
ans[arr[i][2]]=binaryseach(arr,i,n-1,arr[i][1]);
141+
}
142+
143+
144+
return ans;
145+
}
146+
147+
public int binaryseach(int [][] arr,int low,int high,int target){
148+
149+
150+
int result = -1;
151+
152+
while (low <= high) {
153+
int mid = low + (high - low) / 2;
154+
155+
if (arr[mid][0] >= target) {
156+
result = arr[mid][2];
157+
high = mid - 1;
158+
} else {
159+
low = mid + 1;
160+
}
161+
}
162+
return result;
163+
164+
165+
}
166+
}
167+
```
168+
169+
</TabItem>
170+
<TabItem value="python" label="Python">
171+
<SolutionAuthor name="@Shreyash3087"/>
172+
173+
```python
174+
from typing import List
175+
176+
class Solution:
177+
def findRightInterval(self, intv: List[List[int]]) -> List[int]:
178+
n = len(intv)
179+
arr = [[0, 0, 0] for _ in range(n)]
180+
181+
for i in range(n):
182+
arr[i][0] = intv[i][0]
183+
arr[i][1] = intv[i][1]
184+
arr[i][2] = i
185+
186+
arr.sort(key=lambda x: x[0])
187+
188+
ans = [0] * n
189+
for i in range(n):
190+
ans[arr[i][2]] = self.binary_search(arr, i, n - 1, arr[i][1])
191+
192+
return ans
193+
194+
def binary_search(self, arr: List[List[int]], low: int, high: int, target: int) -> int:
195+
result = -1
196+
197+
while low <= high:
198+
mid = low + (high - low) // 2
199+
200+
if arr[mid][0] >= target:
201+
result = arr[mid][2]
202+
high = mid - 1
203+
else:
204+
low = mid + 1
205+
206+
return result
207+
208+
```
209+
</TabItem>
210+
</Tabs>
211+
212+
## Complexity Analysis
213+
214+
### Time Complexity: $O(Nlog(N))$
215+
216+
> **Reason**: The time complexity of the algorithm is O(Nlog(N)), where N is the number of intervals. Sorting the array takes O(Nlog(N)), and for each interval, a binary search takes O(log(N)), leading to O(Nlog(N)) overall due to the N binary searches.
217+
218+
### Space Complexity: $O(N)$
219+
220+
> **Reason**: This space is used for the transformed array and the output array.
221+
222+
## References
223+
224+
- **LeetCode Problem**: [Find Right Interval](https://leetcode.com/problems/find-right-interval/description/)
225+
226+
- **Solution Link**: [Find Right Interval](https://leetcode.com/problems/find-right-interval/solutions/)

0 commit comments

Comments
 (0)