-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)に落ちると思う。 | ||
|
|
||
| ### step2 | ||
|
|
||
| 特に変更点なし | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||||||||||||||
| // 最初このコードを書いてTLE | ||||||||||||||||||
|
|
||||||||||||||||||
| class Solution { | ||||||||||||||||||
| public: | ||||||||||||||||||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||||||||||||||||||
| priority_queue<pair<int, vector<int>>> kth_smallest; | ||||||||||||||||||
| for (int num1 : nums1) { | ||||||||||||||||||
| for (int num2 : nums2) { | ||||||||||||||||||
| kth_smallest.push({num1 + num2, { num1, num2 }}); | ||||||||||||||||||
| if (kth_smallest.size() > k) kth_smallest.pop(); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| vector<vector<int>> answer; | ||||||||||||||||||
| while (!kth_smallest.empty()) { | ||||||||||||||||||
| answer.push_back(kth_smallest.top().second); | ||||||||||||||||||
| kth_smallest.pop(); | ||||||||||||||||||
| } | ||||||||||||||||||
| return answer; | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| // Naoto Iwaseさんのコードを参考に修正したもの | ||||||||||||||||||
|
|
||||||||||||||||||
| class Solution { | ||||||||||||||||||
| public: | ||||||||||||||||||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||||||||||||||||||
| priority_queue< | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pair<int, pair<int,int>> は、どこに何が入っているのか分かりにくく感じました。 struct を定義し、 operator<() を定義してあげたほうが、読み手にとって読みやすくなると思います。 |
||||||||||||||||||
| pair<int, pair<int,int>>, | ||||||||||||||||||
| vector<pair<int, pair<int,int>>>, | ||||||||||||||||||
| greater<> | ||||||||||||||||||
| > smallest; | ||||||||||||||||||
|
|
||||||||||||||||||
| for (int j = 0; j < nums2.size() && j < k; ++j) { | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この方法だと、どのペアを既に追加したかを記録する必要がなくなってスマートですね。 |
||||||||||||||||||
| smallest.push({nums1[0] + nums2[j], {0, j}}); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}); |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| vector<vector<int>> answer; | ||||||||||||||||||
|
|
||||||||||||||||||
| while (!smallest.empty() && answer.size() < k) { | ||||||||||||||||||
| auto [sum, nums] = smallest.top(); | ||||||||||||||||||
| smallest.pop(); | ||||||||||||||||||
| int num1 = nums.first; | ||||||||||||||||||
| int num2 = nums.second; | ||||||||||||||||||
|
|
||||||||||||||||||
| answer.push_back({nums1[num1], nums2[num2]}); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (num1 + 1 < nums1.size()) { | ||||||||||||||||||
| smallest.push({nums1[num1 + 1] + nums2[num2], {num1 + 1, num2}}); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+46
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たしかに3回書いてるので変数にしてもいいかもしれないですね。 |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return answer; | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. step2,3と比べ、こちらの改行があるコードスタイルの方が読みやすいと感じました。変数の宣言、操作を行うコードが適切にグループ分けされているので読みやすいという感覚です。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たしかに改行は多少はあった方が読みやすいかもしれないです。 |
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 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; | ||
| for (int i = 0; i < nums2.size() && i < k; i++) { | ||
| smallest.push({nums1[0] + nums2[i], {0, i}}); | ||
| } | ||
| vector<vector<int>> answer; | ||
| while (!smallest.empty() && answer.size() < k) { | ||
| auto [sum, nums] = smallest.top(); | ||
| smallest.pop(); | ||
| int num1 = nums.first, num2 = nums.second; | ||
| answer.push_back({nums1[num1], nums2[num2]}); | ||
| if (num1 + 1 < nums1.size()) { | ||
| smallest.push({nums1[num1 + 1] + nums2[num2], {num1 + 1, num2}}); | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3つ要素を含有するのであればtuple<int, int, int> か structを作るのも良いのかと思いました。 |
||
| for (int i = 0; i < nums2.size(); i++) { | ||
| smallest.push({nums1[0] + nums2[i], {0, i}}); | ||
| } | ||
| vector<vector<int>> answer; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. answer.reserve(k) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @austyhooong さん
|
||
| while (!smallest.empty() && answer.size() < k) { | ||
| auto [sum, nums] = smallest.top(); | ||
| smallest.pop(); | ||
| int num1 = nums.first; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. auto [num1, num2] = nums; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @austyhooong さん |
||
| int num2 = nums.second; | ||
| answer.push_back({nums1[num1], nums2[num2]}); | ||
| if (num1 + 1 < nums1.size()) { | ||
| smallest.push({nums1[num1 + 1] + nums2[num2], {num1 + 1, num2}}); | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
| }; | ||
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.
こちらのコメントを参照されることをお勧めいたします。
Yuto729/leetcode#16 (comment)