373. Find K Pairs with Smallest Sums#10
Open
tarinaihitori wants to merge 1 commit into
Open
Conversation
oda
reviewed
Oct 26, 2024
| all_num_pairs = [] | ||
| for nums1_i, num_1 in enumerate(nums1): | ||
| for nums2_i, num_2 in enumerate(nums2): | ||
| min_heap.append((num_1 + num_2, nums1_i, nums2_i)) |
oda
reviewed
Oct 26, 2024
|
|
||
| all_num_pairs.sort(key=lambda x: x[0]) | ||
| top_k_pairs = all_num_pairs[:k] | ||
| return [[nums1[i], nums2[j]] for _, i, j in top_k_pairs] |
There was a problem hiding this comment.
ここで、nums1[i] の変換をするならば、上で for num_1 in nums1: として、append((num_1 + num_2, num_1, num_2)) でもよいかもしれません。
oda
reviewed
Oct 26, 2024
Comment on lines
+55
to
+58
| if nums2_i == 0 and nums1_i + 1 < len(nums1): | ||
| heapq.heappush(sum_with_each_index, (nums1[nums1_i + 1] + nums2[0], nums1_i + 1, 0)) | ||
| if nums2_i + 1 < len(nums2): | ||
| heapq.heappush(sum_with_each_index, (nums1[nums1_i] + nums2[nums2_i + 1], nums1_i, nums2_i + 1)) |
There was a problem hiding this comment.
私は、個人的にはここ2行ずつ関数にしてしまいたいです。
繰り返しが多いと間違うので。
oda
reviewed
Oct 26, 2024
|
|
||
| 2nd | ||
| heap にはそれぞれの最初の要素の和だけを入れておき、随時追加する。 | ||
| https://github.com/hayashi-ay/leetcode/pull/66 |
There was a problem hiding this comment.
私は書くことよりも人のコードを読むことが大事だと思っております。
過去に書いた人のコードを見比べて、口に合う合わないを考えて書いておくといいと思います。
|
良さそうに思いました! |
hayashi-ay
reviewed
Oct 27, 2024
Comment on lines
+67
to
+68
| sum_with_each_index = [] | ||
| heapq.heappush(sum_with_each_index, (nums1[0] + nums2[0], 0, 0)) |
There was a problem hiding this comment.
まあ好みかもですが、sum_with_each_index = [(nums1[0] + nums2[0], 0, 0)]で良いですね。
nittoco
reviewed
Nov 9, 2024
| class Solution: | ||
| def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: | ||
| sum_with_each_index = [] | ||
| result = [] |
There was a problem hiding this comment.
細かいんですが、resultは使う直前(L31のへん)に定義した方が、目の移動が少なくて見やすいかもです
| result = [] | ||
|
|
||
| for i in range(min(k, len(nums1))): | ||
| heapq.heappush(sum_with_each_index, (nums1[i] + nums2[0], i, 0)) |
There was a problem hiding this comment.
これくらいだとわかりやすいので微妙なラインですが、タブルの中身が多いときは、中身がわかりやすいようにdataclassにしてもいいかもしれません
|
見ました |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/