Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f1f9c5

Browse files
Create 436._Find_Right_Interval.md
1 parent e7be8cf commit 0f1f9c5

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 436. Find Right Interval
2+
3+
**<font color=red>难度: Medium</font>**
4+
5+
## 刷题内容
6+
7+
> 原题连接
8+
9+
* https://leetcode.com/problems/find-right-interval/description/
10+
11+
> 内容描述
12+
13+
```
14+
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
15+
16+
For any interval i, you need to store the minimum interval j's index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.
17+
18+
Note:
19+
You may assume the interval's end point is always bigger than its start point.
20+
You may assume none of these intervals have the same start point.
21+
Example 1:
22+
Input: [ [1,2] ]
23+
24+
Output: [-1]
25+
26+
Explanation: There is only one interval in the collection, so it outputs -1.
27+
Example 2:
28+
Input: [ [3,4], [2,3], [1,2] ]
29+
30+
Output: [-1, 0, 1]
31+
32+
Explanation: There is no satisfied "right" interval for [3,4].
33+
For [2,3], the interval [3,4] has minimum-"right" start point;
34+
For [1,2], the interval [2,3] has minimum-"right" start point.
35+
Example 3:
36+
Input: [ [1,4], [2,3], [3,4] ]
37+
38+
Output: [-1, 2, -1]
39+
40+
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
41+
For [2,3], the interval [3,4] has minimum-"right" start point.
42+
```
43+
44+
## 解题方案
45+
46+
> 思路 1
47+
******- 时间复杂度: O(N^2)******- 空间复杂度: O(N)******
48+
49+
50+
打算先按照start,end排个序,然后只要往后面找就行,虽然是O(N^2),但是稍微小了点,结果超时。
51+
52+
```
53+
class Solution(object):
54+
def findRightInterval(self, intervals):
55+
"""
56+
:type intervals: List[Interval]
57+
:rtype: List[int]
58+
"""
59+
res = [-1] * len(intervals)
60+
new_intervals = sorted(intervals, key = lambda x: (x.start, x.end))
61+
lookup = {}
62+
for idx, interval in enumerate(intervals):
63+
lookup[(interval.start, interval.end)] = idx
64+
65+
for i in range(len(new_intervals)):
66+
for j in range(i+1, len(new_intervals)):
67+
if new_intervals[j].start >= new_intervals[i].end:
68+
look_interval = (new_intervals[i].start, new_intervals[i].end)
69+
look_idx = lookup[look_interval]
70+
next_interval = (new_intervals[j].start, new_intervals[j].end)
71+
next_idx = lookup[next_interval]
72+
res[look_idx] = next_idx
73+
break
74+
return res
75+
```
76+
77+
> 思路 2
78+
******- 时间复杂度: O(N^2)******- 空间复杂度: O(N)******
79+
80+
后面我想到既然所有的start都不一样,我不如搞一个list存排过序的start,然后这样就可以用二分查找来找到第一个大于某end的idx了,
81+
同样的用一个字典来存start和idx的信息
82+
83+
```python
84+
```
85+
86+
87+
88+
89+
90+
91+
## Follow up
92+
93+
94+
就是题目变成
95+
```
96+
Given a set of intervals, for each of the interval i,
97+
check if there exists an interval j whose start point is bigger than or equal to the start point of the interval i,
98+
which can be called that j is on the "right" of i.
99+
```
100+
101+
102+
> 思路 1
103+
******- 时间复杂度: O(NlgN)******- 空间复杂度: O(N)******
104+
105+
106+
```python
107+
class Solution(object):
108+
def findRightInterval(self, intervals):
109+
"""
110+
:type intervals: List[Interval]
111+
:rtype: List[int]
112+
"""
113+
res = [-1] * len(intervals)
114+
new_intervals = sorted(intervals, key = lambda x: (x.start, x.end))
115+
lookup = {}
116+
for idx, interval in enumerate(intervals):
117+
lookup[(interval.start, interval.end)] = idx
118+
for i in range(len(new_intervals)-1):
119+
look_interval = (new_intervals[i].start, new_intervals[i].end)
120+
look_idx = lookup[look_interval]
121+
next_interval = (new_intervals[i+1].start, new_intervals[i+1].end)
122+
next_idx = lookup[next_interval]
123+
res[look_idx] = next_idx
124+
return res
125+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /