Skip to content

973. K Closest Points to Origin#28

Open
ryosuketc wants to merge 1 commit intomainfrom
973_k_closest_points_to_origin
Open

973. K Closest Points to Origin#28
ryosuketc wants to merge 1 commit intomainfrom
973_k_closest_points_to_origin

Conversation

@ryosuketc
Copy link
Owner

vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
// (distance, point pair)
std::priority_queue<std::pair<int, vector<int>>> k_closest_points;
for (auto point : points) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
5ky7/arai60#22 (comment)

std::vector<std::vector<int>> kClosest(std::vector<std::vector<int>>& points, int k) {
// note: the arg (point) is edited, you may want to copy
// std::vector<std::vector<int>> points_copy = points;
std::sort(points.begin(), points.end(), isCloser);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数 (のポインター) を渡すと、インライン化されにくくなるようです。代わりに関数オブジェクトを渡すことをお勧めいたします。ラムダ式は関数オブジェクトのため、インライン化されやすいようです。
https://timsong-cpp.github.io/cppwp/n3337/expr.prim.lambda#3

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type — whose properties are described below.

// note: the arg (point) is edited, you may want to copy
// std::vector<std::vector<int>> points_copy = points;
std::sort(points.begin(), points.end(), [](const std::vector<int>& a, const std::vector<int>& b){
return (a[0] * a[0] + a[1] * a[1]) < (b[0] * b[0] + b[1] * b[1]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a[0] * a[0] + a[1] * a[1] が 2 回登場するため、関数化したほうがすっきりするかもしれません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants