Skip to content

373. Find K Pairs with Smallest Sums#11

Open
hemispherium wants to merge 1 commit intomainfrom
0373-find-k-pair-with-smallest-sums
Open

373. Find K Pairs with Smallest Sums#11
hemispherium wants to merge 1 commit intomainfrom
0373-find-k-pair-with-smallest-sums

Conversation

@hemispherium
Copy link
Owner

@hemispherium hemispherium self-assigned this Dec 16, 2025

return answer;
}
};
Copy link

Choose a reason for hiding this comment

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

step2,3と比べ、こちらの改行があるコードスタイルの方が読みやすいと感じました。変数の宣言、操作を行うコードが適切にグループ分けされているので読みやすいという感覚です。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに改行は多少はあった方が読みやすいかもしれないです。

Comment on lines +46 to +49

if (num1 + 1 < nums1.size()) {
smallest.push({nums1[num1 + 1] + nums2[num2], {num1 + 1, num2}});
}
Copy link

Choose a reason for hiding this comment

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

num1 + 1が3回出現しているので変数にまとめたいと思いました。変数名next_num1としてみたのですが、あまり良い変数名ではないかもしれません。重複するコードの内、意味が同じものであれば変数にまとめることで、可読性や保守性を向上できるという感覚です。

Suggested change
if (num1 + 1 < nums1.size()) {
smallest.push({nums1[num1 + 1] + nums2[num2], {num1 + 1, num2}});
}
int next_num1 = num1 + 1;
if (next_num1 < nums1.size()) {
smallest.push({nums1[next_num1] + nums2[num2], {next_num1, num2}});
}

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに3回書いてるので変数にしてもいいかもしれないですね。

@@ -0,0 +1,11 @@
### step1

最初nums1, nums2の組み合わせを全探索してpriority_queueに入れて、小さい順に取り出す方法を思いついてTLE思想だと思ったが、それ以外思いつかなかったのでそれで実装。Naoto Iwaseさんのコードを参考に修正。nums1はindex 0のものとnums2はk個目までの和を最初にpriority_queueに入れておいて、popするたびにnums1のindexをインクリメントしたものをpriority_queueにpushするという実装。これで時間計算量はO(nums1 * nums2)からO(k)に落ちると思う。
Copy link

Choose a reason for hiding this comment

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

こちらのコメントを参照されることをお勧めいたします。
Yuto729/LeetCode_arai60#16 (comment)

> smallest;

for (int j = 0; j < nums2.size() && j < k; ++j) {
smallest.push({nums1[0] + nums2[j], {0, j}});
Copy link

Choose a reason for hiding this comment

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

std::pair には、要素を引数に取るコンストラクターがあります。これを利用し、 emplace() で std::pair に格納される要素を直接渡して追加しても良いと思います。

smallest.emplace(nums1[0] + nums2[j], {0, j});

class Solution {
public:
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
priority_queue<
Copy link

Choose a reason for hiding this comment

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

pair<int, pair<int,int>> は、どこに何が入っているのか分かりにくく感じました。 struct を定義し、 operator<() を定義してあげたほうが、読み手にとって読みやすくなると思います。
スコープが十分に短ければ、pair<int, pair<int,int>> でも問題ないと思います。

while (!smallest.empty() && answer.size() < k) {
auto [sum, nums] = smallest.top();
smallest.pop();
int num1 = nums.first;

Choose a reason for hiding this comment

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

auto [num1, num2] = nums;

Copy link

Choose a reason for hiding this comment

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

@austyhooong さん
レビューコメントにコードスニペットだけを書くのは、やや不躾なように感じました。「おすすめします」といたフレーズを添えると、語調が和らぎ、読み手に取って不快感を与えないのではないかと思います。

for (int i = 0; i < nums2.size(); i++) {
smallest.push({nums1[0] + nums2[i], {0, i}});
}
vector<vector<int>> answer;

Choose a reason for hiding this comment

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

answer.reserve(k)

Copy link

Choose a reason for hiding this comment

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

@austyhooong さん
reserve() を呼び出すべきかどうかについて、過去に複数のレビューコメントが書かれています。無条件に reserve() を呼び出すことを促すのではなく、過去のレビューコメントを勘案したうえで、呼び出すべきかどうかについてコメントを書かれることをお勧めいたします。

class Solution {
public:
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<>> smallest;

Choose a reason for hiding this comment

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

3つ要素を含有するのであればtuple<int, int, int> か structを作るのも良いのかと思いました。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants