Skip to content

Commit 7300749

Browse files
committed
0986 solved.
1 parent 84e6ec5 commit 7300749

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
add_executable(C main.cpp)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/// Source : https://leetcode.com/problems/interval-list-intersections/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-04
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
12+
/// Ad-Hoc
13+
/// Time Complexity: O(m + n)
14+
/// Space Complexity: O(1)
15+
16+
/// Definition for an interval.
17+
struct Interval {
18+
int start;
19+
int end;
20+
Interval() : start(0), end(0) {}
21+
Interval(int s, int e) : start(s), end(e) {}
22+
};
23+
24+
class Solution {
25+
public:
26+
vector<Interval> intervalIntersection(vector<Interval>& A, vector<Interval>& B) {
27+
28+
vector<Interval> res;
29+
30+
int i = 0, j = 0;
31+
while(i < A.size() && j < B.size()){
32+
33+
int l = max(A[i].start, B[j].start);
34+
int h = min(A[i].end, B[j].end);
35+
if(l <= h)
36+
res.push_back(Interval(l, h));
37+
38+
if(A[i].end <= B[j].end)
39+
i ++;
40+
else
41+
j ++;
42+
}
43+
return res;
44+
}
45+
};
46+
47+
48+
void print_vec(const vector<Interval>& vec){
49+
50+
for(const Interval& interval: vec)
51+
cout << "(" << interval.start << "," << interval.end << ") ";
52+
cout << endl;
53+
}
54+
55+
int main() {
56+
57+
vector<Interval> A1 = {Interval(0,2),Interval(5,10),Interval(13,23),Interval(24,25)};
58+
vector<Interval> B1 = {Interval(1,5),Interval(8,12),Interval(15,24),Interval(25,26)};
59+
print_vec(Solution().intervalIntersection(A1, B1));
60+
// [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
61+
62+
vector<Interval> A2 = {Interval(5,10)};
63+
vector<Interval> B2 = {Interval(5,10)};
64+
print_vec(Solution().intervalIntersection(A2, B2));
65+
// [[5, 10]]
66+
67+
vector<Interval> A3 = {Interval(3,5), Interval(9, 20)};
68+
vector<Interval> B3 = {Interval(4,5), Interval(7, 10), Interval(11, 12), Interval(14, 15), Interval(16, 20)};
69+
print_vec(Solution().intervalIntersection(A3, B3));
70+
// [[4,5],[9,10],[11,12],[14,15],[16,20]]
71+
72+
return 0;
73+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
641641
| 983 | [Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/) | [solution](https://leetcode.com/problems/minimum-cost-for-tickets/solution/) | [C++](0983-Minimum-Cost-For-Tickets/cpp-0983/) | | |
642642
| 984 | [String Without AAA or BBB](https://leetcode.com/problems/string-without-aaa-or-bbb/) | [solution](https://leetcode.com/problems/string-without-aaa-or-bbb/solution/) | [C++](0984-String-Without-AAA-or-BBB/cpp-0984/) | | |
643643
| 985 | [Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/) | [solution](https://leetcode.com/problems/sum-of-even-numbers-after-queries/) | [C++](0985-Sum-of-Even-Numbers-After-Queries/cpp-0985/) | | |
644-
| | | | | | |
644+
| 986 | [Interval List Intersections](https://leetcode.com/problems/interval-list-intersections/) | [solution](https://leetcode.com/problems/interval-list-intersections/solution/) | [C++](0986-Interval-List-Intersections/cpp-0986/) | | |
645645
| 987 | [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/) | [solution](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/solution/) | [C++](0987-Vertical-Order-Traversal-of-a-Binary-Tree/cpp-0987/) | | |
646646
| 988 | [Smallest String Starting From Leaf](https://leetcode.com/problems/smallest-string-starting-from-leaf/) | [solution](https://leetcode.com/problems/smallest-string-starting-from-leaf/solution/) | [C++](0988-Smallest-String-Starting-From-Leaf/cpp-0988/) | | |
647647
| | | | | | |

0 commit comments

Comments
 (0)