-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNumberOfDiscIntersections.cpp
More file actions
74 lines (72 loc) · 2.36 KB
/
NumberOfDiscIntersections.cpp
File metadata and controls
74 lines (72 loc) · 2.36 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
74
//https://codility.com/demo/results/trainingWY56F4-XBX/
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
/*
* I didn't get 100 for the first try. I ignored the overflow possibility when I calcuate the length of intervals.
* Well, looks like it's always a good idea to add some extreme numbers into your own test cases.
*/
int solutionNumberOfDiscIntersections(const vector<int>& A)
{
int len = A.size();
if (0 == len)return 0;
vector<std::pair<long long, long long> > intervals;
for (int i = 0; i < len; ++i)
intervals.push_back(make_pair(i - (long long)A[i], i + (long long)A[i]));
sort(intervals.begin(), intervals.end());
int l, h, m, count=0;
for (int i = 0; i < len - 1; ++i)
{
l = i + 1;
h = len - 1;
while (l <= h)
{
m = l + (h - l) / 2;
if (intervals[i].second >= intervals[m].first)
l = m + 1;
else
h = m - 1;
}
count += l - 1 - i;
if (count > 10000000)
return -1;
}
return count;
}
int solutionNumberOfDiscIntersections(vector<int> &&A)
{
int len = A.size();
if(0 == len)return 0;
vector<std::pair<long, long> > segments;
for(int i = 0; i < len; ++i)
segments.emplace_back(std::make_pair((long)i - (long)A[i], (long)i + (long)A[i]));
std::sort(segments.begin(), segments.end(), [](pair<long, long> p1, pair<long, long> p2){return p1.first < p2.first;});
int count = 0, low, high, mid, idx;
for(int i = 0; i < len; ++i)
{
low = i, high = len - 1, idx = i;
while(low <= high)
{
mid = low + (high - low) / 2;
if(segments[mid].first <= segments[i].second)
{
idx = mid;
low = mid + 1;
}
else
high = mid - 1;
}
count += idx - i;
if(count > 1e7) return -1;
}
return count;
}
void testNumberOfDiscIntersections()
{
cout << "Expect 11: " << solutionNumberOfDiscIntersections(vector<int>({ 1, 5, 2, 1, 4, 0 })) << endl;
cout << "Expect 0: " << solutionNumberOfDiscIntersections(vector<int>({})) << endl;
cout << "Expect 0: " << solutionNumberOfDiscIntersections(vector<int>({1})) << endl;
cout << "Expect 2: " << solutionNumberOfDiscIntersections(vector<int>({ 0, 1, 1 })) << endl;
cout << "Expect 2: " << solutionNumberOfDiscIntersections(vector<int>({ 1, 2147483647, 0 })) << endl;
}