Skip to content

Commit 42da039

Browse files
committed
2020-04-12 436. Find Right Interval Solution
1 parent 98943ab commit 42da039

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# LeetCode 436. Find Right Interval
2+
3+
## Description
4+
5+
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.
6+
7+
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.
8+
9+
Note:
10+
11+
You may assume the interval's end point is always bigger than its start point.
12+
You may assume none of these intervals have the same start point.
13+
14+
15+
Example 1:
16+
17+
```py
18+
Input: [ [1,2] ]
19+
20+
Output: [-1]
21+
22+
Explanation: There is only one interval in the collection, so it outputs -1.
23+
```
24+
25+
Example 2:
26+
27+
```py
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+
```
36+
37+
Example 3:
38+
39+
```py
40+
Input: [ [1,4], [2,3], [3,4] ]
41+
42+
Output: [-1, 2, -1]
43+
44+
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
45+
For [2,3], the interval [3,4] has minimum-"right" start point.
46+
```
47+
48+
### 思路
49+
50+
* 所有的区间的结尾不重复,因此构造一个字典,健为区间的开始位置,值为区间在原数组中的索引。
51+
* 将所有的区间的开始位置取出来形成一个数组 starts ,对数组按照从小到大排序。
52+
* 对于一个区间,记此区间结尾为 end,查找在 starts 数组中第一个大于等于 end 的数所在的位置 t,t 即为满足条件的区间。
53+
* 根据 t ,找到 t 在字典中对应的值即可确定区间的位置;对所有的区间都进行此操作。
54+
55+
```py
56+
# -*- coding: utf-8 -*-
57+
# @Author: 何睿
58+
# @Create Date: 2020-04-11 16:08:04
59+
# @Last Modified by: 何睿
60+
# @Last Modified time: 2020-04-11 16:34:06
61+
62+
63+
from typing import List
64+
65+
66+
class Solution:
67+
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
68+
starts = []
69+
index_dict = {}
70+
for index, interval in enumerate(intervals):
71+
starts.append(interval[0])
72+
index_dict[interval[0]] = index
73+
74+
starts.sort()
75+
76+
return list(self._binary_find(starts, interval[1], index_dict) for interval in intervals)
77+
78+
def _binary_find(self, nums, target, index_dict):
79+
80+
if target in index_dict:
81+
return index_dict[target]
82+
83+
left, right = 0, len(nums) - 1
84+
middle = left + (right - left) // 2
85+
while left <= right:
86+
if nums[middle] >= target and (middle == 0 or nums[middle - 1] < target):
87+
return index_dict[nums[middle]]
88+
elif nums[middle] < target:
89+
left = middle + 1
90+
elif nums[middle] >= target and (middle == 0 or nums[middle - 1] >= target):
91+
right = middle - 1
92+
middle = left + (right - left) // 2
93+
94+
return -1
95+
```
96+
源代码文件在 [这里](https://github.com/ruicore/Algorithm/blob/master/LeetCode/2020-04-11-436-Find-Right-Interval.py) 。
97+
©本文首发于 何睿的博客 ,欢迎转载,转载需保留 [文章来源](https://ruicore.cn/leetcode-436-find-right-interval/) ,作者信息和本声明.

0 commit comments

Comments
 (0)