forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (55 loc) · 1.85 KB
/
main.cpp
File metadata and controls
73 lines (55 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// Source : https://leetcode.com/problems/interval-list-intersections/
/// Author : liuyubobobo
/// Time : 2019-02-04
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(m + n)
/// Space Complexity: O(1)
/// Definition for an interval.
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
class Solution {
public:
vector<Interval> intervalIntersection(vector<Interval>& A, vector<Interval>& B) {
vector<Interval> res;
int i = 0, j = 0;
while(i < A.size() && j < B.size()){
int l = max(A[i].start, B[j].start);
int h = min(A[i].end, B[j].end);
if(l <= h)
res.push_back(Interval(l, h));
if(A[i].end <= B[j].end)
i ++;
else
j ++;
}
return res;
}
};
void print_vec(const vector<Interval>& vec){
for(const Interval& interval: vec)
cout << "(" << interval.start << "," << interval.end << ") ";
cout << endl;
}
int main() {
vector<Interval> A1 = {Interval(0,2),Interval(5,10),Interval(13,23),Interval(24,25)};
vector<Interval> B1 = {Interval(1,5),Interval(8,12),Interval(15,24),Interval(25,26)};
print_vec(Solution().intervalIntersection(A1, B1));
// [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
vector<Interval> A2 = {Interval(5,10)};
vector<Interval> B2 = {Interval(5,10)};
print_vec(Solution().intervalIntersection(A2, B2));
// [[5, 10]]
vector<Interval> A3 = {Interval(3,5), Interval(9, 20)};
vector<Interval> B3 = {Interval(4,5), Interval(7, 10), Interval(11, 12), Interval(14, 15), Interval(16, 20)};
print_vec(Solution().intervalIntersection(A3, B3));
// [[4,5],[9,10],[11,12],[14,15],[16,20]]
return 0;
}