Triangle

problem

solution

注意避免 overflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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;
#include <algorithm>
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
sort(A.begin(), A.end());
int n = A.size();
for(int i=1;i<n-1;++i){
if(
A[i-1] > A[i+1] - A[i]
// A[i+1] + A[i-1] > A[i] && \
// A[i] + A[i+1] > A[i-1] && \

) return true;
}
return false;
}

analysis

  • time complexity O(nlogn)
  • space complexity O(1)