Skip to content

Commit f1976ac

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (91.54%), Memory - 18.6 MB (12.89%)
1 parent 1529ac2 commit f1976ac

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>You are given two lists of closed intervals, <code>firstList</code> and <code>secondList</code>, where <code>firstList[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> and <code>secondList[j] = [start<sub>j</sub>, end<sub>j</sub>]</code>. Each list of intervals is pairwise <strong>disjoint</strong> and in <strong>sorted order</strong>.</p>
2+
3+
<p>Return <em>the intersection of these two interval lists</em>.</p>
4+
5+
<p>A <strong>closed interval</strong> <code>[a, b]</code> (with <code>a &lt;= b</code>) denotes the set of real numbers <code>x</code> with <code>a &lt;= x &lt;= b</code>.</p>
6+
7+
<p>The <strong>intersection</strong> of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of <code>[1, 3]</code> and <code>[2, 4]</code> is <code>[2, 3]</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/interval1.png" style="width: 700px; height: 194px;" />
12+
<pre>
13+
<strong>Input:</strong> firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
14+
<strong>Output:</strong> [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> firstList = [[1,3],[5,9]], secondList = []
21+
<strong>Output:</strong> []
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>0 &lt;= firstList.length, secondList.length &lt;= 1000</code></li>
29+
<li><code>firstList.length + secondList.length &gt;= 1</code></li>
30+
<li><code>0 &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
31+
<li><code>end<sub>i</sub> &lt; start<sub>i+1</sub></code></li>
32+
<li><code>0 &lt;= start<sub>j</sub> &lt; end<sub>j</sub> &lt;= 10<sup>9</sup> </code></li>
33+
<li><code>end<sub>j</sub> &lt; start<sub>j+1</sub></code></li>
34+
</ul>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Approach 1: Merge Intervals
2+
3+
# M, N = lengths of `firstList` and `secondList` respectively
4+
# Time: O(M + N)
5+
# Space: O(M + N)
6+
7+
class Solution:
8+
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
9+
ans = []
10+
i = j = 0
11+
12+
while i < len(firstList) and j < len(secondList):
13+
# Let's check if firstList[i] intersects secondList[j].
14+
# lo - the startpoint of the intersection
15+
# hi - the endpoint of the intersection
16+
lo = max(firstList[i][0], secondList[j][0])
17+
hi = min(firstList[i][1], secondList[j][1])
18+
if lo <= hi:
19+
ans.append([lo, hi])
20+
21+
# Remove the interval with the smallest endpoint
22+
if firstList[i][1] < secondList[j][1]:
23+
i += 1
24+
else:
25+
j += 1
26+
27+
return ans

0 commit comments

Comments
 (0)