-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.3_CountTriangles.cpp
More file actions
39 lines (32 loc) · 982 Bytes
/
15.3_CountTriangles.cpp
File metadata and controls
39 lines (32 loc) · 982 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
/*
First attempt was almost perfect. Performance was the only problem, but even then it was the right complexity level and
it took 0.11s instead of 0.1s because of a special case where all input values are equal. Because this is stupid, I'm
including both reports for posterity:
90%: https://codility.com/demo/results/trainingNS75Y8-KTF/
100%: https://codility.com/demo/results/training2YYB7A-7XV/
*/
#include <algorithm>
int solution(vector<int> &A) {
if (A.empty() || A.size() < 3)
return 0;
int N = A.size();
sort(A.begin(), A.end());
if ( A[0] == A[N-1] )
return (N * (N-1) * (N-2))/6;
int back = 0, middle = 1, front = 2, trips=0;
while ( back < N-2 ) {
while ( middle < N-1) {
while ( A[back] + A[middle] > A[front] && front < N) {
//cout << "(" << back << ", " << middle << ", " << front << ")" << endl;
trips++;
front++;
}
middle++;
front = middle+1;
}
back++;
middle = back+1;
front = back+2;
}
return trips;
}