Open
Conversation
nodchip
reviewed
Jan 10, 2026
| 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) { |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
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); |
There was a problem hiding this comment.
関数 (のポインター) を渡すと、インライン化されにくくなるようです。代わりに関数オブジェクトを渡すことをお勧めいたします。ラムダ式は関数オブジェクトのため、インライン化されやすいようです。
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]); |
There was a problem hiding this comment.
a[0] * a[0] + a[1] * a[1] が 2 回登場するため、関数化したほうがすっきりするかもしれません。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
973. K Closest Points to Origin
https://leetcode.com/problems/k-closest-points-to-origin/