-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoOfIntersection.cpp
39 lines (30 loc) · 1.02 KB
/
NoOfIntersection.cpp
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
// 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;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
vector<int> diskStartPoint(A.size());
vector<int> diskEndPoint(A.size());
size_t noOfDisks = A.size();
int openDisks = 0;
int interSections = 0;
int endPointIdx = 0;
for(size_t i = 0; i < noOfDisks; i++) {
diskStartPoint[i] = i - A[i];
diskEndPoint[i] = i + A[i];
}
std::sort(diskStartPoint.begin(), diskStartPoint.end());
std::sort(diskEndPoint.begin(), diskEndPoint.end());
for(size_t i = 0; i < noOfDisks;) {
if(diskStartPoint[i] <= diskEndPoint[endPointIdx]) {
interSections += openDisks;
openDisks++;
i++;
} else {
endPointIdx++;
openDisks--;
}
}
return interSections;
}