코테
[Codility] Triangle C++
29도 맑음
2022. 1. 7. 21:12
반응형
이문제는
제일 큰 길이가 나머지 두개의 합보다 크면 무조건 삼각수가 된다
그래서 정렬후 제일큰값<그거보다 1단계작은값+2단계작은값 이면 1을 리턴했다.
여기서 문제는 범위가 [−2,147,483,648..2,147,483,647]라는 것이다.
저범위가 int의 끝이기 때문에 제일큰 값을을 더하면 int범위를 넘어가게 돼서 long long으로 해야한다.
#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)
if(A.size()<3) return 0;
sort(A.begin(), A.end());
for(int i=A.size()-1; i>1; i--){
long long n = (long long)A[i-1]+A[i-2];
if(A[i]<n){
return 1;
}
}
return 0;
}
반응형