Conversation
| @@ -0,0 +1,18 @@ | |||
| class Solution: | |||
There was a problem hiding this comment.
3 phase目では1 phase目の解答だと空間計算量の節約にはなるんですが既にみたindexが重複していないかなどをレビュワーに考えさせるのは時間かかるだろうなと思ったのでseenという変数で明示的に見たiとjは考慮してないよということを強調した解答にしました
| while len(k_smallest_pairs) < k: | ||
| _, i, j = heapq.heappop(min_heap) | ||
| k_smallest_pairs.append([nums1[i], nums2[j]]) | ||
| if i + 1 < len(nums1) and (i + 1, j) not in seen: |
| def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: | ||
| k_smallest_pairs = [] | ||
| min_heap = [] | ||
| for j in range(min(k, len(nums2))): |
There was a problem hiding this comment.
min(k, len(num2))として空間計算量を省略することほかの問題で使えそうですね。
それと、for iがないのにfor j があるのは若干違和感があります。
少し後ろを読めばわかるので、良いのかもしれませんが。
| class Solution: | ||
| def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: | ||
| k_smallest_pairs = [] | ||
| min_heap = [] |
There was a problem hiding this comment.
minとsmallestは同じような意味だと思うのですが、使い分けた理由があれば教えていただきたいです
There was a problem hiding this comment.
最小ヒープを構成する配列なのでmin heapだとよく使われるしわかりやすいかなと思いました
| if j + 1 < len(nums2) and (i, j + 1) not in seen: | ||
| heapq.heappush(min_heap, (nums1[i] + nums2[j + 1], i, j + 1)) | ||
| seen.add((i, j + 1)) | ||
|
|
There was a problem hiding this comment.
(i+1, j)と(i, j+1)の組み合わせで同じ操作をしているので、関数化してあげた方が自然で拡張性も高いコードになるのかなと思いました。
| if i + 1 < len(nums1): | ||
| heapq.heappush(min_heap, (nums1[i + 1] + nums2[j], i + 1, j)) # nums1[i + 1]とnums2[j]のpairが次のk smallestとなりうる | ||
|
|
||
| return k_smallest_pairs |
There was a problem hiding this comment.
わかりやすかったです。
強いていうなら、min(k, len(nums2))でやりたいことができなくなってしまいますが、
9行目を for i, num in enumerate(nums2): で初期処理をする方が、
和と index を使った min_heap をこれから操作していくイメージを読み手に与えられるかなと思いました。
| heapq.heappush(min_heap, (nums1[0] + nums2[0], 0, 0)) | ||
|
|
||
| while len(k_smallest_pairs) < k: | ||
| _, i, j = heapq.heappop(min_heap) | ||
| k_smallest_pairs.append([nums1[i], nums2[j]]) | ||
| if i + 1 < len(nums1) and (i + 1, j) not in seen: | ||
| heapq.heappush(min_heap, (nums1[i + 1] + nums2[j], i + 1, j)) | ||
| seen.add((i + 1, j)) | ||
| if j + 1 < len(nums2) and (i, j + 1) not in seen: | ||
| heapq.heappush(min_heap, (nums1[i] + nums2[j + 1], i, j + 1)) |
There was a problem hiding this comment.
まずは関数化が良いかと思いましたが、頭の heapq も省略できるので、さらに実装が軽くなるかもです。
Fuminiton/LeetCode#8 (comment)
問題: https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/