-
Notifications
You must be signed in to change notification settings - Fork 0
solved find_k_pairs_with_smallest_sums #44
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: arai60
Are you sure you want to change the base?
Conversation
| @@ -0,0 +1,18 @@ | |||
| class Solution: | |||
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.
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: |
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.
ここは同じ操作をするので関数化するほうが好きです
| 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.
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 = [] |
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.
minとsmallestは同じような意味だと思うのですが、使い分けた理由があれば教えていただきたいです
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.
最小ヒープを構成する配列なのでmin heapだとよく使われるしわかりやすいかなと思いました
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.
なるほど、ありがとうございます。
| 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.
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)の組み合わせで同じ操作をしているので、関数化してあげた方が自然で拡張性も高いコードになるのかなと思いました。
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.
その場合、(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 |
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.
わかりやすかったです。
強いていうなら、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.
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)
問題: https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/