Conversation
|
|
||
| return answer; | ||
| } | ||
| }; |
There was a problem hiding this comment.
step2,3と比べ、こちらの改行があるコードスタイルの方が読みやすいと感じました。変数の宣言、操作を行うコードが適切にグループ分けされているので読みやすいという感覚です。
There was a problem hiding this comment.
たしかに改行は多少はあった方が読みやすいかもしれないです。
|
|
||
| if (num1 + 1 < nums1.size()) { | ||
| smallest.push({nums1[num1 + 1] + nums2[num2], {num1 + 1, num2}}); | ||
| } |
There was a problem hiding this comment.
num1 + 1が3回出現しているので変数にまとめたいと思いました。変数名next_num1としてみたのですが、あまり良い変数名ではないかもしれません。重複するコードの内、意味が同じものであれば変数にまとめることで、可読性や保守性を向上できるという感覚です。
| 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}}); | |
| } |
There was a problem hiding this comment.
たしかに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)に落ちると思う。 | |||
There was a problem hiding this comment.
こちらのコメントを参照されることをお勧めいたします。
Yuto729/LeetCode_arai60#16 (comment)
| > smallest; | ||
|
|
||
| for (int j = 0; j < nums2.size() && j < k; ++j) { | ||
| smallest.push({nums1[0] + nums2[j], {0, j}}); |
There was a problem hiding this comment.
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< |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
@austyhooong さん
レビューコメントにコードスニペットだけを書くのは、やや不躾なように感じました。「おすすめします」といたフレーズを添えると、語調が和らぎ、読み手に取って不快感を与えないのではないかと思います。
| 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.
@austyhooong さん
reserve() を呼び出すべきかどうかについて、過去に複数のレビューコメントが書かれています。無条件に reserve() を呼び出すことを促すのではなく、過去のレビューコメントを勘案したうえで、呼び出すべきかどうかについてコメントを書かれることをお勧めいたします。
- Create 49. Group Anagrams Apo-Matchbox/LeetCode_Practice#24 (comment)
- Create KthLargestElementInAStream.md kt-from-j/leetcode#14 (comment)
- 6. Zigzag Conversion Ryotaro25/leetcode_first60#66 (comment)
- solve: 929. Unique Email Addresses t9a-dev/LeetCode_arai60#4 (comment)
- 102. Binary Tree Level Order Traversal huyfififi/coding-challenges#31 (comment)
- 125. Valid Palindrome ryosuketc/leetcode_grind75#5 (comment)
| 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.
3つ要素を含有するのであればtuple<int, int, int> か structを作るのも良いのかと思いました。
この問題:https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/
次に解く問題:https://leetcode.com/problems/group-anagrams/description/