373. Find K Pairs with Smallest Sums - #54
Open
skypenguins wants to merge 1 commit into
Open
Conversation
oda
reviewed
Jul 26, 2026
|
|
||
| return flatten_pairs[:k] | ||
| ``` | ||
| - 制約上 `nums1`, `nums2` はそれぞれ最大 $10^{5}$ 個の要素を持ちうるため、最悪ケースでは $10^{10}$ 個の組をメモリに保持しようとすることになる |
miyataka
reviewed
Jul 26, 2026
| heapq.heappush(candidates, (nums1[i] + nums2[0], i, 0)) | ||
|
|
||
| pairs = [] | ||
| while candidates is not None and len(pairs) < k: |
There was a problem hiding this comment.
step2と比較して, is not None が追加されていますが,これはなくてもよいように感じました.
意図があれば教えて欲しいです.
Owner
Author
There was a problem hiding this comment.
Truthy/Falsyな値の評価であることを強調する意図を込めています。
ksaito0629/leetcode_arai60#1 (comment) のコメントにあるように、 Google Python Style Guideline を参考にしました。
kazizi55
reviewed
Jul 28, 2026
|
|
||
| ## Step3 | ||
| ```py | ||
| class Solution: |
There was a problem hiding this comment.
興味本位なのですが、step2 で多くの解法を見た上でこの解法を選択した理由は何になりますでしょうか?
みた感じ、len(nums1) が小さく、len(nums2)がかなり大きい場合とかに適している解法なのかなとは思ったのですが。
nodchip
reviewed
Jul 30, 2026
| ## Step1 | ||
| ### 方針 | ||
| - `num1`, `num2` は昇順であることが保証されているため、 `347. Top K Frequent Elements` とほぼ同じ方針で解いてみる | ||
| - MLEになる。後ろの方の組はどう考えても不要だなと思いつつも、この時点で20分経過していたため正答を見る |
There was a problem hiding this comment.
コードを書く前に空間計算量を求め、おおよそのメモリ使用量を求めることをお勧めいたします。おおよそのメモリ使用量は、空間計算量に入力データサイズの上限を代入し、 1 要素当たりのバイト数を掛けると求められます。 1 要素当たりのバイト数は、 sys.getsizeof() で調べることができます。ぜひ試してみて下さい。
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.
373. Find K Pairs with Smallest Sums
次回予告: 560. Subarray Sum Equals K