Skip to content

Commit d5e1e21

Browse files
committed
Interval List Intersections
1 parent 797b6eb commit d5e1e21

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

interval-list-intersections.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Runtime: 64 ms
2+
// Memory Usage: 18.8 MB
3+
class Solution {
4+
public:
5+
vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
6+
vector<vector<int>> res;
7+
int i = 0;
8+
int j = 0;
9+
while (i < A.size() && j < B.size()) {
10+
int x = max(A[i][0], B[j][0]);
11+
int y;
12+
if (A[i][1] > B[j][1]) {
13+
y = B[j++][1];
14+
} else {
15+
y = A[i++][1];
16+
}
17+
if (x <= y)
18+
res.push_back({x, y});
19+
}
20+
return res;
21+
}
22+
};

0 commit comments

Comments
 (0)