Skip to content

Commit 90f6afc

Browse files
436. Find Right Interval (Brute Force)
Difficulty: Medium 17 / 17 test cases passed. Runtime: 8264 ms Memory Usage: 19.4 MB
1 parent 3a96f3b commit 90f6afc

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Medium/436.FindRightInterval.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Given a set of intervals, for each of the interval i, check if there exists
3+
an interval j whose start point is bigger than or equal to the end point of
4+
the interval i, which can be called that j is on the "right" of i.
5+
For any interval i, you need to store the minimum interval j's index, which
6+
means that the interval j has the minimum start point to build the "right"
7+
relationship for interval i. If the interval j doesn't exist, store -1 for
8+
the interval i. Finally, you need output the stored value of each interval
9+
as an array.
10+
11+
Note:
12+
1. You may assume the interval's end point is always bigger than its
13+
start point.
14+
2. You may assume none of these intervals have the same start point.
15+
16+
Example:
17+
Input: [ [1,2] ]
18+
Output: [-1]
19+
Explanation: There is only one interval in the collection, so it outputs -1.
20+
21+
Example:
22+
Input: [ [3,4], [2,3], [1,2] ]
23+
Output: [-1, 0, 1]
24+
Explanation: There is no satisfied "right" interval for [3,4].
25+
For [2,3], the interval [3,4] has minimum-"right" start point;
26+
For [1,2], the interval [2,3] has minimum-"right" start point.
27+
28+
Example:
29+
Input: [ [1,4], [2,3], [3,4] ]
30+
Output: [-1, 2, -1]
31+
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
32+
For [2,3], the interval [3,4] has minimum-"right" start point.
33+
34+
NOTE: input types have been changed on April 15, 2019. Please reset to
35+
default code definition to get new method signature.
36+
"""
37+
#Difficulty: Medium
38+
#17 / 17 test cases passed.
39+
#Runtime: 8264 ms
40+
#Memory Usage: 19.4 MB
41+
42+
#Runtime: 8264 ms, faster than 5.18% of Python3 online submissions for Find Right Interval.
43+
#Memory Usage: 19.4 MB, less than 30.05% of Python3 online submissions for Find Right Interval.
44+
45+
class Solution:
46+
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
47+
start = []
48+
end = []
49+
right_intervals = []
50+
for interval in intervals:
51+
start.append(interval[0])
52+
end.append(interval[1])
53+
for endpoint in end:
54+
if endpoint in start:
55+
right_intervals.append(start.index(endpoint))
56+
elif endpoint <= max(start):
57+
while endpoint <= max(start):
58+
endpoint += 1
59+
if endpoint in start:
60+
right_intervals.append(start.index(endpoint))
61+
break
62+
else:
63+
right_intervals.append(-1)
64+
return right_intervals

0 commit comments

Comments
 (0)