Skip to content

373. Find K Pairs with Smallest Sums - #54

Open
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-373
Open

373. Find K Pairs with Smallest Sums#54
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-373

Conversation

@skypenguins

Copy link
Copy Markdown
Owner

373. Find K Pairs with Smallest Sums

次回予告: 560. Subarray Sum Equals K

@skypenguins skypenguins self-assigned this Jul 25, 2026
Comment thread memo.md

return flatten_pairs[:k]
```
- 制約上 `nums1`, `nums2` はそれぞれ最大 $10^{5}$ 個の要素を持ちうるため、最悪ケースでは $10^{10}$ 個の組をメモリに保持しようとすることになる

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私はこれ、手でやる時にどうしますか、だとは思いますね。

Comment thread memo.md
heapq.heappush(candidates, (nums1[i] + nums2[0], i, 0))

pairs = []
while candidates is not None and len(pairs) < k:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step2と比較して, is not None が追加されていますが,これはなくてもよいように感じました.
意図があれば教えて欲しいです.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Truthy/Falsyな値の評価であることを強調する意図を込めています。
ksaito0629/leetcode_arai60#1 (comment) のコメントにあるように、 Google Python Style Guideline を参考にしました。

Comment thread memo.md

## Step3
```py
class Solution:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

興味本位なのですが、step2 で多くの解法を見た上でこの解法を選択した理由は何になりますでしょうか?
みた感じ、len(nums1) が小さく、len(nums2)がかなり大きい場合とかに適している解法なのかなとは思ったのですが。

Comment thread memo.md
## Step1
### 方針
- `num1`, `num2` は昇順であることが保証されているため、 `347. Top K Frequent Elements` とほぼ同じ方針で解いてみる
- MLEになる。後ろの方の組はどう考えても不要だなと思いつつも、この時点で20分経過していたため正答を見る

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コードを書く前に空間計算量を求め、おおよそのメモリ使用量を求めることをお勧めいたします。おおよそのメモリ使用量は、空間計算量に入力データサイズの上限を代入し、 1 要素当たりのバイト数を掛けると求められます。 1 要素当たりのバイト数は、 sys.getsizeof() で調べることができます。ぜひ試してみて下さい。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants