Skip to content

373. Find K Pairs with Smallest Sums#10

Open
tarinaihitori wants to merge 1 commit into
mainfrom
373-find-k-pairs-with-smallest-sums
Open

373. Find K Pairs with Smallest Sums#10
tarinaihitori wants to merge 1 commit into
mainfrom
373-find-k-pairs-with-smallest-sums

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@colorbox colorbox left a comment

Choose a reason for hiding this comment

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

見ました、だいたい良さそうに思います。

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

Choose a reason for hiding this comment

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

min_heap 定義されてません。


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]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここで、nums1[i] の変換をするならば、上で for num_1 in nums1: として、append((num_1 + num_2, num_1, num_2)) でもよいかもしれません。

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

Choose a reason for hiding this comment

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

私は、個人的にはここ2行ずつ関数にしてしまいたいです。
繰り返しが多いと間違うので。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

少々過剰に範囲チェックをしてもいいでしょう。


2nd
heap にはそれぞれの最初の要素の和だけを入れておき、随時追加する。
https://github.com/hayashi-ay/leetcode/pull/66
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は書くことよりも人のコードを読むことが大事だと思っております。
過去に書いた人のコードを見比べて、口に合う合わないを考えて書いておくといいと思います。

@goto-untrapped
Copy link
Copy Markdown

良さそうに思いました!
解法によっては時間計算量や空間計算量も書いておくといいかもしれません。

Comment on lines +67 to +68
sum_with_each_index = []
heapq.heappush(sum_with_each_index, (nums1[0] + nums2[0], 0, 0))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

まあ好みかもですが、sum_with_each_index = [(nums1[0] + nums2[0], 0, 0)]で良いですね。

class Solution:
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
sum_with_each_index = []
result = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいんですが、resultは使う直前(L31のへん)に定義した方が、目の移動が少なくて見やすいかもです

result = []

for i in range(min(k, len(nums1))):
heapq.heappush(sum_with_each_index, (nums1[i] + nums2[0], i, 0))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これくらいだとわかりやすいので微妙なラインですが、タブルの中身が多いときは、中身がわかりやすいようにdataclassにしてもいいかもしれません

@hroc135
Copy link
Copy Markdown

hroc135 commented Nov 14, 2024

見ました
個人的には最初にインデックス(0,0)のものだけヒープに入れる方法の方が好みだと思いました

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.

7 participants