Skip to content

Conversation

@SuperHotDogCat
Copy link
Owner

@@ -0,0 +1,18 @@
class Solution:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは同じ操作をするので関数化するほうが好きです

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))):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = []
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minとsmallestは同じような意味だと思うのですが、使い分けた理由があれば教えていただきたいです

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最小ヒープを構成する配列なのでmin heapだとよく使われるしわかりやすいかなと思いました

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、ありがとうございます。

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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i+1, j)と(i, j+1)の組み合わせで同じ操作をしているので、関数化してあげた方が自然で拡張性も高いコードになるのかなと思いました。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

その場合、(0, 0) もまとめられますね。

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

わかりやすかったです。

強いていうなら、min(k, len(nums2))でやりたいことができなくなってしまいますが、
9行目を for i, num in enumerate(nums2): で初期処理をする方が、
和と index を使った min_heap をこれから操作していくイメージを読み手に与えられるかなと思いました。

Comment on lines +6 to +15
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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まずは関数化が良いかと思いましたが、頭の heapq も省略できるので、さらに実装が軽くなるかもです。
Fuminiton/LeetCode#8 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants