Skip to content

Conversation

@kazukiii
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/

次に解く問題
1. Two Sum

README.mdへ頭の中の言語化と記録をしています。

@kazukiii kazukiii force-pushed the arai60/find-k-pairs-with-smallest-sums branch from 49386df to 79430e2 Compare June 15, 2024 02:47
struct SumPair {
int sum;
vector<int> pair;
vector<int> indice;
Copy link

Choose a reason for hiding this comment

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

pair<int, int> で良くないでしょうか。

Copy link
Owner Author

@kazukiii kazukiii Jun 16, 2024

Choose a reason for hiding this comment

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

たしかにここは vector<int> より pair<int, int> の方が適切かと思いました。
step4で修正します。

private:
struct SumPair {
int sum;
vector<int> pair;
Copy link

Choose a reason for hiding this comment

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

pair は標準ライブラリーにある言葉なので避けたほうがよいでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます。変数名をpairから見直します。

Comment on lines +20 to +21
int sum = nums1[index_nums1] + nums2[index_nums2];
min_sum_pairs.push({sum, {nums1[index_nums1], nums2[index_nums2]}, {index_nums1, index_nums2}});
Copy link

Choose a reason for hiding this comment

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

これ関数にしてもいいかもしれません。

うーん、なんとなく同じ記述が繰り返されている気がするので、整理できないでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ここはうまく整理できそうです。step4で見直してみます。

Choose a reason for hiding this comment

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

具体的にはコンストラクタを定義すると良いですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@liquo-rice
ありがとうございます。コンストラクタを定義してみました。-> beec9a4
こちらでイメージ正しいでしょうか?

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

Choose a reason for hiding this comment

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

C++11 以降は tuple<int, int, int> 以前は、pair<int, pair<int, int>> にするのも一つと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。
tupleやpairを使う選択肢もあったのですが、以前この2つは原則使わずstructを定義した方が可読性の観点で良いのではないかというレビューをいただいたことがあり、ここでは意図的にstructを定義しています。自分も現時点ではstructを定義した方が読みやすさの観点で優るのではないかと考えてます。
こちらに関して小田さんの見解も是非伺いたいです。
関連コメントです↓
#10 (comment)
Ryotaro25/leetcode_first60#11 (comment)

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

あ、はい。そうですね。定義したほうが好ましいですね。ただ、単純なコードだと、< が自動的に定義されたりして読む量が減るので、作らなくてもいいのではないかと思うこともあります。

https://en.cppreference.com/w/cpp/container/priority_queue
Compare の変え方を見ておきましょう。

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