Skip to content

373. Find K Pairs with Smallest Sums #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

373. Find K Pairs with Smallest Sums #25

wants to merge 4 commits into from

Conversation

katsukii
Copy link
Owner

@katsukii katsukii commented May 8, 2025

問題

https://leetcode.com/problems/find-k-pairs-with-smallest-sums/

言語

Java

次に解く問題

Group Anagrams

for (int i = 0; i < Math.min(k, nums1.length); i++) {
sumMinHeap.offer(new int[]{i, 0});
}
while (!sumMinHeap.isEmpty() && result.size() < k) {
Copy link

@shintaroyoshida20 shintaroyoshida20 May 9, 2025

Choose a reason for hiding this comment

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

自分だったら、こう書くかなと思いました。

このwhile文が、resultという配列がサイズKになるまで、要素を追加する
というのが伝わりやすくなるためです。

また、sumMinHeapがemptyになるのは、異常系だと思うので、
if/breakにしたほうが読みやすいと思いました。

        while (result.size() < k) {
            if (sumMinHeap.isEmpty()) {
                break
            }
            ...省略
        }

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。たしかにこちらの方がわかりやすいですね。

Comment on lines +21 to +22
- ブルートフォースで全ペア分の配列を用意し、各ペアの合計値の heap に格納する
- result 用の配列に k 回分 heap から poll()して格納

Choose a reason for hiding this comment

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

Javascriptでこの方法をとった際に、
Time Limit Errorが出て失敗していました。

スクリプト言語とコンパイラ言語の違いかもしれません。

Satorien/LeetCode#12 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

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

わかりづらくすみません。

その後、以下のようにブルートフォースで実装。書きながらだめそうと思ったがやはり Memory Limit Exceeded で動かなかった。

上記に記載のとおりこれも同じく動かなかったコードです。

- ヒープの頂点を取り出し結果配列に追加する
- ポイントは、常に候補の中から最小のマスが探索対象となること。このため合理的な順で探索が進められる

- コメント: Python の tuple みたいな Pair というクラスが Java にあるのを初めて知った
Copy link

Choose a reason for hiding this comment

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

Java 詳しくないのですが、これは標準ではないとかではなかったでしたっけ。

Copy link
Owner Author

Choose a reason for hiding this comment

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

調べたら、Java8までは標準で、その後外されたみたいです。
クラスを自作した方が無難そうですね。


- [i, j]を[0, 0], [0, 1], [1, 1] と進めた後に[1, 0]に戻れない

- その後、以下のようにブルートフォースで実装。書きながらだめそうと思ったがやはり Memory Limit Exceeded で動かなかった。ブルートフォースはだいたいのケースで筋が悪いっぽい
Copy link

Choose a reason for hiding this comment

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

空間計算量は求めましたか?また、空間計算量から、おおよそのメモリ使用量を求めましたか?

おおよそのメモリ使用量は、空間計算量を求めたあと、最大サイズを代入し、要素のおおよそのバイト数を掛けると出てきます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

nums1.length=m、nums2.length=n とした場合、空間計算量は O(m·n) になりますね。

次にメモリ使用量の概算ですが、最悪ケースで m = n = 10^5 ⇒ m·n = 10^10 組 を保持します。

調べてみたところ、
Integer オブジェクト:16B → ペアで32B

よって、32B * 10 ^ 10 = 約320GB になります。現実的ではないことがよくわかりますね。。

public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
List<List<Integer>> result = new ArrayList<>();
if (nums1.length == 0 || nums2.length == 0 || k == 0) {
return result;
Copy link

Choose a reason for hiding this comment

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

変数のスコープは短いほうがソースコードが読みやすくなる傾向があると思います。 result のスコープを短くするため、ここは new ArrayList<>() を返し、最後のループの直前で result を宣言するのはいかがでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、たしかにその方が見やすいですね。ありがとうございます。

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