https://app.codility.com/programmers/lessons/15-caterpillar_method/min_abs_sum_of_two/
#include <algorithm>
int solution(vector<int> &A) {
sort(A.begin(),A.end());
int answer = min(abs(A[0] + A[0]), abs(A[A.size()-1]+ A[A.size()-1]));
int left = 0;
int right = A.size()-1;
for(;left <right;){
answer = min(answer,abs(A[left] + A[right]));
if(abs(A[left]) <abs(A[right])){
right--;
answer = min(answer,abs(A[right] + A[right]));
}
else{
left++;
answer = min(answer, abs(A[left] + A[left]));
}
}
return answer;
}
'Codility' 카테고리의 다른 글
[Codility] Caterpillar Method : AbsDistinct (0) | 2021.07.16 |
---|---|
[Codility] Caterpillar method : CountTriangles (0) | 2021.07.16 |
Codility TieRopes C++ (0) | 2021.07.15 |
Codility MaxNonoverlappingSegments C++ (0) | 2021.07.15 |