diff --git a/2112/211226.cpp b/2112/211226.cpp new file mode 100644 index 0000000..53bf434 --- /dev/null +++ b/2112/211226.cpp @@ -0,0 +1,28 @@ +#include +#include + +struct EuclideanDistanceCompare { + bool operator()(const std::vector& lhs, const std::vector& rhs) const { + return lhs[0] * lhs[0] + lhs[1] * lhs[1] < rhs[0] * rhs[0] + rhs[1] * rhs[1]; + } +}; + +class Solution { +public: + static std::vector> kClosest(std::vector>& points, int k) { + std::sort(points.begin(), points.end(), EuclideanDistanceCompare{}); + return {points.begin(), points.begin() + k}; + } +}; + +int main() { + std::vector> s = { + {3, 3}, + {5, -1}, + {-2, 4} + }; + auto ret = Solution::kClosest(s, 2); + for (const auto& i : ret) + std::printf("%d, %d\n", i[0], i[1]); + return 0; +} \ No newline at end of file diff --git a/2112/CMakeLists.txt b/2112/CMakeLists.txt index 45e8cef..d05e6fd 100644 --- a/2112/CMakeLists.txt +++ b/2112/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2112) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2112 211225.cpp) \ No newline at end of file +ADD_EXECUTABLE(2112 211226.cpp) \ No newline at end of file