Skip to content

Commit 5f607cb

Browse files
Create Day 23 Interval List Intersections.cpp
1 parent ebec059 commit 5f607cb

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
PROBLEM:
2+
3+
4+
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
5+
6+
Return the intersection of these two interval lists.
7+
8+
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection
9+
of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval.
10+
For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
11+
12+
13+
Example 1:
14+
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
15+
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
16+
Reminder: The inputs and the desired output are lists of Interval objects, and not arrays or lists.
17+
18+
19+
Note:
20+
21+
1. 0 <= A.length < 1000
22+
2. 0 <= B.length < 1000
23+
3. 0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
24+
25+
26+
27+
28+
SOLUTION:
29+
30+
31+
class Solution {
32+
public:
33+
vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
34+
35+
int i,j,n,m,l,h;
36+
n=A.size();
37+
m=B.size();
38+
39+
vector<vector<int>> v;
40+
41+
i=0;
42+
j=0;
43+
while(i<n && j<m)
44+
{
45+
l=max(A[i][0],B[j][0]);
46+
h=min(A[i][1],B[j][1]);
47+
48+
if(l<=h)
49+
{
50+
v.push_back({l,h});
51+
}
52+
53+
if(A[i][1]<B[j][1])
54+
i++;
55+
else
56+
j++;
57+
}
58+
59+
return v;
60+
}
61+
};

0 commit comments

Comments
 (0)