728x90
반응형
문제 : 해커랭크 Closet Number
https://www.hackerrank.com/challenges/closest-numbers/problem
언어 : C++
// Complete the closestNumbers function below.
vector<int> closestNumbers(vector<int> arr) {
set<int> sorted;
map<int, vector<int>> diffmap;
for(int i = 0 ;i<arr.size();i++)
{
sorted.insert(arr[i]);
}
auto iter = sorted.begin();
int prev = *iter;
iter++;
for(;iter!=sorted.end();iter++)
{
int diff = *iter - prev ;
diffmap[diff].push_back( prev);
diffmap[diff].push_back( *iter);
prev = *iter;
}
return diffmap.begin()->second;
}
728x90
반응형
'개발 > 코딩' 카테고리의 다른 글
[hackerrank] Minimum Absolute Difference in an Array / C++ (0) | 2020.12.07 |
---|---|
LeetCode - Two Sum / c++ (0) | 2020.07.24 |
해커랭크(HackerRank) - The Full Counting Sort / C++ (0) | 2020.07.11 |
해커랭크(HackerRank) - Counting Sort 1,2 / C++ (0) | 2020.07.11 |
해커랭크(HackerRank) - Running Time of Algorithms / C++ (0) | 2020.07.11 |
댓글