-
Notifications
You must be signed in to change notification settings - Fork 0
373. Find K Pairs with Smallest Sums #25
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
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,84 @@ | ||
| /* | ||
| 1時間以上 | ||
|
|
||
| Time: O( log(max(nums1,nums2)) * n log(m) ) | ||
| Space: O( k ) | ||
| n = nums1.size() | ||
| m = nums2.size() | ||
|
|
||
| nums1,nums2のすべてのペアを検証すると時間が足りないので、それを補う方法を考える。 | ||
| ペアの数がk個以下となるような値Lを二分探索で探す。 | ||
| 合計値がL以下のペアをpriority_queueにいれて最小k個のペアを探す。 | ||
| ・二分探索での桁溢れ | ||
| ・同値のペアが大量に作成される | ||
| などの問題を都度修正したら過アンリ時間がかかってしまったがかろうじてパスはしたものの | ||
| 色々と筋悪な回答なのでstep2以降で修正する。 | ||
|
|
||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| long long int left = nums1.front() + nums2.front(); | ||
| long long int right = nums1.back() + nums2.back() + 1; | ||
| long long int mid; | ||
|
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.
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. たしかにここに |
||
| while (left + 1 < right) { | ||
|
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. この二分探索は、 2 つの整数の和が i 以下となるようなペアの数が k 以上となるような最小の i を求めていますよね。その場合、 [false, false, ... false, true, ... true] と並んだ配列のうち、一番左の true の位置を求める問題と帰着できると思います。この問題を解くにあたり、区間は半開区間で考えて良いと思います。半開区間で考えた場合、 count < k の場合は、 mid の位置は false となるので、区間を狭めたときに mid の位置は区間に含めないようにしたいです。そのため、 left = mid + 1 となると思います。また、最終的に left の位置に一番左の true がきますので、以降は left を参照するのが正しいと思います。ご確認いただいてもよろしいでしょうか?
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. ありがとうございます、なるほど、最終的にleftとrightが一致するように二分探索を組むということですね。 |
||
| mid = left + (right - left) / 2; | ||
| int count = count_less_pairs(nums1, nums2, mid, k); | ||
| if (count >= k) { | ||
| right = mid; | ||
| } else { | ||
| left = mid; | ||
| } | ||
| } | ||
|
|
||
| priority_queue<SumPair> sum_pairs; | ||
| for (int num1 : nums1) { | ||
| for (int num2 : nums2) { | ||
| if (num1 + num2 > right) { | ||
|
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. right が何を表しているか分かりづらいため、いちど max_sum 等、別の変数名を割り当てたほうがよいと思います。
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. たしかにrightのまま使うと意味不明ですね |
||
| break; | ||
| } | ||
| sum_pairs.push({num1 + num2, {num1, num2}}); | ||
|
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. emplace
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. ありがとうございます、pushよりもこちらのほうが効率が良さそうですね |
||
| if (sum_pairs.size() > k) { | ||
| sum_pairs.pop(); | ||
| if (num1 + num2 == sum_pairs.top().sum) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| vector<vector<int>> answer; | ||
| while (!sum_pairs.empty()) { | ||
| answer.push_back(sum_pairs.top().pair); | ||
| sum_pairs.pop(); | ||
| } | ||
| return answer; | ||
| } | ||
| private: | ||
|
|
||
| struct SumPair { | ||
| int sum; | ||
| vector<int> pair; | ||
|
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. そのままtuple<int,int,int>使ってもいいのかなと思いました
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. tupleは何が何を意味しているのかが分かりづらくなるので使いませんでした。 |
||
|
|
||
| bool operator< (const SumPair& other) const { | ||
| return sum < other.sum; | ||
| } | ||
| }; | ||
|
|
||
| int count_less_pairs(vector<int> nums1, vector<int>nums2, int num, int limit) { | ||
|
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. ここ、vector が毎回コピーされますね。
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. これは良くないですね・・・ |
||
| int count_sum = 0; | ||
|
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.
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. ありがとうございます、確かに動詞になる単語が先頭にあると関数っぽく見えますね・・・ |
||
| for (int num1 : nums1) { | ||
| auto it = upper_bound(nums2.begin(), nums2.end(), num - num1); | ||
| if (it == nums2.end()) { | ||
|
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. どちらのケースも
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. ありがとうございます、これはdistanceを使用して分岐をなくすほうがシンプルですね。 |
||
| count_sum += nums2.size(); | ||
| if (count_sum > limit) { | ||
| return limit; | ||
| } | ||
| continue; | ||
| } | ||
| int count = it - nums2.begin(); | ||
| count_sum += count; | ||
|
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. このあとには はいらないのでしょうか? 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. たぶん、これ結果的には動きますね。二分探索で、limit 以上の部分と未満の部分の境界を探すという動きをしているので。
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. これは既存のif節を消す、もしくはこの行に を追加するほうが挙動が一貫するので良いですね |
||
| } | ||
| return count_sum; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| step1 after review | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| int64_t left = nums1.front() + nums2.front(); | ||
| int64_t right = nums1.back() + nums2.back() + 1; | ||
| while (left < right) { | ||
| int64_t mid = left + (right - left) / 2; | ||
| int count = count_less_pairs(nums1, nums2, mid, k); | ||
| if (count >= k) { | ||
| right = mid; | ||
| } else { | ||
| left = mid + 1; | ||
| } | ||
| } | ||
|
|
||
| int max_sum = left; | ||
| priority_queue<SumPair> sum_pairs; | ||
| for (int num1 : nums1) { | ||
| for (int num2 : nums2) { | ||
| if (num1 + num2 > max_sum) { | ||
| break; | ||
| } | ||
| sum_pairs.push({num1 + num2, {num1, num2}}); | ||
| if (sum_pairs.size() > k) { | ||
| sum_pairs.pop(); | ||
| if (num1 + num2 == sum_pairs.top().sum) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| vector<vector<int>> answer; | ||
| while (!sum_pairs.empty()) { | ||
| answer.push_back(sum_pairs.top().pair); | ||
| sum_pairs.pop(); | ||
| } | ||
| return answer; | ||
| } | ||
| private: | ||
|
|
||
| struct SumPair { | ||
| int sum; | ||
| vector<int> pair; | ||
|
|
||
| bool operator< (const SumPair& other) const { | ||
| return sum < other.sum; | ||
| } | ||
| }; | ||
|
|
||
| int count_less_pairs(vector<int> nums1, vector<int>nums2, int num, int limit) { | ||
| int num_pairs = 0; | ||
| for (int num1 : nums1) { | ||
| auto it = upper_bound(nums2.begin(), nums2.end(), num - num1); | ||
| num_pairs += distance(nums2.begin(), it); | ||
| if (num_pairs > limit) { | ||
| return limit; | ||
| } | ||
| } | ||
| return num_pairs; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| 他の方の回答を参考にやり直したもの | ||
| nums1,nums2のインデックスのペアをダイクストラ法っぽく処理していく | ||
| Time: O(k logk) | ||
| Space: O(k) | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| priority_queue<SumIndex, vector<SumIndex>, greater<SumIndex>> sum_index; | ||
|
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. greater を使わず、 operator < を定義したほうがシンプルだと思います。
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. デフォルトで最小値を取り出す なにをもって大小関係とするかは定義によるので、シンプルにできる方を選択するという理解をしました。 |
||
| set<vector<int>> visited; | ||
| vector<vector<int>> answer; | ||
| sum_index.push({nums1[0] + nums2[0], {0, 0}}); | ||
| while (answer.size() < k) { | ||
| int i = sum_index.top().index[0]; | ||
| int j = sum_index.top().index[1]; | ||
| sum_index.pop(); | ||
| answer.push_back({nums1[i], nums2[j]}); | ||
| if (i + 1 < nums1.size() && !visited.contains({i + 1, j})) { | ||
| sum_index.push({nums1[i + 1] + nums2[j], {i + 1, j}}); | ||
| visited.insert({i + 1, j}); | ||
| } | ||
| if (j + 1 < nums2.size() && !visited.contains({i, j + 1})) { | ||
| sum_index.push({nums1[i] + nums2[j + 1], {i, j + 1}}); | ||
| visited.insert({i, j + 1}); | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| private: | ||
| struct SumIndex { | ||
| int sum; | ||
| vector<int> index; | ||
|
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. vector は値を格納するにあたりヒープメモリを確保するため、定数倍重くなります。避けられるのであれば避け、 step3_2 のように、固定長配列や、変数複数個を使ったほうがよいと思います。
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. なるほど、ありがとうございます。 |
||
|
|
||
| bool operator> (const SumIndex& other) const { | ||
|
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. https://brevzin.github.io/c++/2019/07/28/comparisons-cpp20/
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. ありがとうございます、読んでおきます。 |
||
| return sum > other.sum; | ||
| } | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| step2 after review | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| priority_queue<SumIndex> sum_index; | ||
| set<vector<int>> visited; | ||
| vector<vector<int>> answer; | ||
| sum_index.push({nums1[0] + nums2[0], {0, 0}}); | ||
| while (answer.size() < k) { | ||
| int i = sum_index.top().index[0]; | ||
| int j = sum_index.top().index[1]; | ||
| sum_index.pop(); | ||
| answer.push_back({nums1[i], nums2[j]}); | ||
| if (i + 1 < nums1.size() && !visited.contains({i + 1, j})) { | ||
| sum_index.push({nums1[i + 1] + nums2[j], {i + 1, j}}); | ||
| visited.insert({i + 1, j}); | ||
| } | ||
| if (j + 1 < nums2.size() && !visited.contains({i, j + 1})) { | ||
| sum_index.push({nums1[i] + nums2[j + 1], {i, j + 1}}); | ||
| visited.insert({i, j + 1}); | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| private: | ||
| struct SumIndex { | ||
| int sum; | ||
| vector<int> index; | ||
|
|
||
| bool operator< (const SumIndex& other) const { | ||
| return sum > other.sum; | ||
| } | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| struct不使用パターン | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> sum_index; | ||
|
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. 要素を vector にすると、何番目の要素が何を表しているのか把握しにくく、読みにくく感じます。構造体を定義し、 operator < を定義してあげたほうが、読んでいてわかりやすいと思います。
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. ありがとうございます、たしかにそのとおりですね。 |
||
| set<vector<int>> visited; | ||
| vector<vector<int>> answer; | ||
| sum_index.push({0, 0, 0}); | ||
| while (answer.size() < k && sum_index.size() > 0) { | ||
|
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. 個人の好み的なところもありますが、不等号の向きを<で統一するという方針もありますね
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. ありがとうございます、統一しておくと読みやすくなりそうですね。 |
||
| int i = sum_index.top()[1]; | ||
| int j = sum_index.top()[2]; | ||
| sum_index.pop(); | ||
| answer.push_back({nums1[i], nums2[j]}); | ||
| if (i + 1 < nums1.size() && !visited.contains({i + 1, 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. 少し引数の管理が難しいですがこの処理は大体共通なので外だしする手もあるかとおもいます
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. ここは外に出すと逆に複雑になってしまいそうなので、共通化などせずにこのようにしています。 |
||
| sum_index.push({nums1[i + 1] + nums2[j], i + 1, j}); | ||
| visited.insert({i + 1, j}); | ||
| } | ||
| if (j + 1 < nums2.size() && !visited.contains({i, j + 1})) { | ||
|
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. ちなみにvisitedを使わない方法もありますのでご参考までに
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. ありがとうございます、参考にさせていただきます。 |
||
| sum_index.push({nums1[i] + nums2[j + 1], i, j + 1}); | ||
| visited.insert({i, j + 1}); | ||
| } | ||
| } | ||
| 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. 手前味噌ですが、先に片方の配列の先頭要素と、もう片方の配列の要素全ての pair を priority queue をに入れてから処理を続ける方法もありますので参考までに: seal-azarashi/leetcode#10 (comment)
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,39 @@ | ||
| /* | ||
| structを使用したパターン | ||
| priority_queueやstructの宣言で若干冗長に感じるが、こちらのほうが読みやすい | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| priority_queue<SumIndex, vector<SumIndex>, greater<SumIndex>> sum_index; | ||
| set<vector<int>> visited; | ||
| vector<vector<int>> minimum_k_pairs; | ||
| sum_index.push({nums1[0] + nums2[0], 0, 0}); | ||
|
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. 個人的には、sumとindexが入っていることはここを見る(もしくはstructの中身を見る)かすればすぐわかるので、やや冗長に感じました。
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. なるほど、ありがとうございます。 |
||
| while (minimum_k_pairs.size() < k && sum_index.size() > 0) { | ||
| int i = sum_index.top().num1_index; | ||
| int j = sum_index.top().num2_index; | ||
| sum_index.pop(); | ||
| minimum_k_pairs.push_back({nums1[i], nums2[j]}); | ||
| if (i + 1 < nums1.size() && !visited.contains({i + 1, j})) { | ||
| sum_index.push({nums1[i + 1] + nums2[j], i+1, j}); | ||
| visited.insert({i + 1, j}); | ||
| } | ||
| if (j + 1 < nums2.size() && !visited.contains({i, j + 1})) { | ||
| sum_index.push({nums1[i] + nums2[j + 1], i, j + 1}); | ||
| visited.insert({i, j + 1}); | ||
| } | ||
| } | ||
| return minimum_k_pairs; | ||
| } | ||
|
|
||
| private: | ||
| struct SumIndex { | ||
| int sum; | ||
| int num1_index; | ||
| int num2_index; | ||
|
|
||
| bool operator> (const SumIndex& other) const { | ||
| return sum > other.sum; | ||
| } | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| {i + 1, j}と{i, j + 1}をpriority_queueにいれる時に、{i + 1, j - 1}と{i - 1, j + 1}がvisitedに含まれているかを確認して効率化するパターン | ||
| 条件判定部分が無駄に複雑なので関数化したいが、いい感じにできないのでそのままにしている | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| priority_queue<SumIndex, vector<SumIndex>, greater<SumIndex>> sum_index; | ||
| vector<vector<int>> minimum_k_pair; | ||
| set<vector<int>> visited; | ||
| sum_index.push({nums1[0] + nums2[0], 0, 0}); | ||
| while (minimum_k_pair.size() < k && sum_index.size() > 0) { | ||
| int i = sum_index.top().num1_index; | ||
| int j = sum_index.top().num2_index; | ||
|
Comment on lines
+13
to
+14
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. SumIndex では num~_index と命名されているものが i, j となるのに少し違和感を感じました。while 文の中でだけ、頭の中で変数名マップを張っていないと正確に実装が理解出来ないので、読みづらくなりそうだなという印象です。
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.
Comment on lines
+13
to
+14
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. https://en.cppreference.com/w/cpp/language/structured_binding
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. なるほど、ありがとうございます。 |
||
| sum_index.pop(); | ||
| minimum_k_pair.push_back({nums1[i], nums2[j]}); | ||
| if (i + 1 < nums1.size() && !visited.contains({i + 1, j}) && (j == 0 || visited.contains({i + 1, j - 1}))) { | ||
| sum_index.push({nums1[i + 1] + nums2[j], i + 1, j}); | ||
| visited.insert({i + 1, j}); | ||
| } | ||
| if (j + 1 < nums2.size() && !visited.contains({i, j + 1}) && (i == 0 || visited.contains({i - 1, j + 1}))) { | ||
| sum_index.push({nums1[i] + nums2[j + 1], i, j + 1}); | ||
| visited.insert({i, j + 1}); | ||
| } | ||
| } | ||
| return minimum_k_pair; | ||
|
Comment on lines
+8
to
+26
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. 結構行数が多いので、ある程度の粒度で関数に切り出したり、空白行が入っているともっと見通しが良くなるなと思いました。 |
||
| } | ||
|
|
||
| private: | ||
| struct SumIndex { | ||
| int sum; | ||
| int num1_index; | ||
| int num2_index; | ||
|
|
||
| bool operator> (const SumIndex& other) const { | ||
| return sum > other.sum; | ||
| } | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| {i + 1, j}と{i, j + 1}をpriority_queueにいれる時に、{i + 1, j - 1}と{i - 1, j + 1}がvisitedに含まれているかを確認して効率化するパターン | ||
| 条件判定部分が無駄に複雑なので関数化したいが、いい感じにできないのでそのままにしている | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | ||
| priority_queue<SumIndex, vector<SumIndex>, greater<SumIndex>> sum_index; | ||
| vector<vector<int>> minimum_k_pair; | ||
| set<vector<int>> visited; | ||
| sum_index.push({nums1[0] + nums2[0], 0, 0}); | ||
| while (minimum_k_pair.size() < k && sum_index.size() > 0) { | ||
| int i = sum_index.top().num1_index; | ||
| int j = sum_index.top().num2_index; | ||
| sum_index.pop(); | ||
| minimum_k_pair.push_back({nums1[i], nums2[j]}); | ||
| if (i + 1 < nums1.size() && !visited.contains({i + 1, j}) && (j == 0 || visited.contains({i + 1, j - 1}))) { | ||
| sum_index.push({nums1[i + 1] + nums2[j], i + 1, j}); | ||
| visited.insert({i + 1, j}); | ||
| } | ||
| if (j + 1 < nums2.size() && !visited.contains({i, j + 1}) && (i == 0 || visited.contains({i - 1, j + 1}))) { | ||
| sum_index.push({nums1[i] + nums2[j + 1], i, j + 1}); | ||
| visited.insert({i, j + 1}); | ||
| } | ||
| } | ||
| return minimum_k_pair; | ||
| } | ||
|
|
||
| private: | ||
| struct SumIndex { | ||
| int sum; | ||
| int num1_index; | ||
| int num2_index; | ||
|
|
||
| bool operator> (const SumIndex& other) const { | ||
| return sum > other.sum; | ||
| } | ||
| }; | ||
| }; |
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.
整数は 2 つまでしか足されず、 -109 <= num <= 109 のため、 int の範囲を超えることはありません。よって int で十分だと思います。また、 long long int はあまり見かけません。古いコードだと long long、比較的新しいコードだと、ビット幅が決まっている int64_t を使うように思います。
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.
ありがとうございます、こちらintで十分ですね。
int64_t勉強になります。
https://cpprefjp.github.io/reference/cstdint/int64_t.html