-
Notifications
You must be signed in to change notification settings - Fork 0
373. Find K Pairs with Smallest Sums #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
49386df to
79430e2
Compare
| struct SumPair { | ||
| int sum; | ||
| vector<int> pair; | ||
| vector<int> indice; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pair<int, int> で良くないでしょうか。
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pair は標準ライブラリーにある言葉なので避けたほうがよいでしょう。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘ありがとうございます。変数名をpairから見直します。
| int sum = nums1[index_nums1] + nums2[index_nums2]; | ||
| min_sum_pairs.push({sum, {nums1[index_nums1], nums2[index_nums2]}, {index_nums1, index_nums2}}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ関数にしてもいいかもしれません。
うーん、なんとなく同じ記述が繰り返されている気がするので、整理できないでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここはうまく整理できそうです。step4で見直してみます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
具体的にはコンストラクタを定義すると良いですね。
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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>> にするのも一つと思います。
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 の変え方を見ておきましょう。
問題へのリンク
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/
次に解く問題
1. Two Sum
README.mdへ頭の中の言語化と記録をしています。