Count the number of triangles that can be built from a given set of edges.
Solution
#include <algorithm>int solution(vector<int> &A) { // write your code in C++11 (g++ 4.8.2) int s = int(A.size()); if (s < 3) return 0; sort(A.begin(), A.end()); if (A[0] + A[1] > A[s - 1]) { return s * (s - 1) * (s -2) / 6; } int num = 0; int start = 0; int end = 0; for (int i = 0; i < s -2; i++) { for (int j = i + 1; j < s - 1; j++) { for (int k = j + 1; k < s; k++) { if (A[i] + A[j] > A[k]) { num++; } else { break; } } } } return num;}