-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfDiscIntersections2
More file actions
44 lines (35 loc) · 975 Bytes
/
NumberOfDiscIntersections2
File metadata and controls
44 lines (35 loc) · 975 Bytes
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
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
struct Interval {
long left;
long right;
};
inline bool mySortFunction(Interval p1, Interval p2) {
return ( p1.left < p2.left );
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
if (A.empty()) {
return 0;
}
vector<Interval> arr;
for (size_t i = 0; i < A.size(); ++i) {
arr.push_back({ (long)(i - A[i]), long(i + A[i]) });
}
std::sort(arr.begin(), arr.end(), mySortFunction);
int result = 0;
for (size_t i = 0; i < arr.size(); ++i) {
for (size_t j = i + 1; j < arr.size(); ++j)
{
if (arr[j].left > arr[i].right) {
break;
}
if (++result > 10000000) {
return -1;
}
}
}
return result;
}