Skip to content

373. Find K Pairs With Smallest Sums#12

Open
xbam326 wants to merge 3 commits into
mainfrom
373
Open

373. Find K Pairs With Smallest Sums#12
xbam326 wants to merge 3 commits into
mainfrom
373

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Jan 9, 2026

解いた問題
Find K Pairs With Smallest Sums

次に解く問題
Two Sum

@xbam326 xbam326 changed the title 373 373. Find K Pairs With Smallest Sums Jan 9, 2026
self, nums1: List[int], nums2: List[int], k: int
) -> List[List[int]]:
candidate = []
added_indice = set()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

add はsetの操作なので、実際の意味にできるとよりよいのではないでしょうか。すぐに思いついたのは used, あるいは検証するという意味で checked, tested などj

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.

ご指摘ありがとうございます。
毎回実際の意味にできておらずデータ構造への操作の名前にしてしまうよう癖づいてしまっていることを認識できました。
候補を挙げていただいた中だと特にcheckedがしっくりきました。

enqueue_if_valid_and_not_added(0, 0)

while len(smallest_pairs) < k:
_sum, [n1_index, n2_index] = heapq.heappop(candidate)
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_, 単に無視するなら _, 和であることを示しながら無視するなら sum_ignored などの選択肢もありそうです。

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.

元々は無視をしていい変数だが和が入っていますというニュアンスで_sumとしておりました。
sumが入っていることは今回のケースだと意識しなくて良いので今度は_とするようにいたします。
またsumが入っていることをどうしても示したい時はsum_ignoredとするようにします。

- `空間計算量を求めたあと、実際のどの程度のメモリを使用するか推定されましたでしょうか?もししていないのであれば、推定することをお勧めいたします。推定するには、時間計算量の式に最大サイズを代入し、 1 要素当たりのサイズを掛けてみてください。`. `sys.getsizeof`など勉強になった
- 自分で言うところのsum_heapは最小値候補なので、`candidates`とした方が良いように思った
- 自分の命名だと意味が付け加えられていないように感じた
- `enqueue_if_valid_and_not_visited`確かにわかりやすい
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

関数名はこれくらいしつこいものを付けても構わないと思います。

Copy link
Copy Markdown

@naoto-iwase naoto-iwase left a comment

Choose a reason for hiding this comment

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

stepを経るごとにどんどん読みやすくなるように感じました。

step3のコードで問題ないと思います。

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.

4 participants