Skip to content

373. Find K Pairs with Smallest Sums#17

Merged
ryoooooory merged 15 commits intomainfrom
task/373
Aug 23, 2024
Merged

373. Find K Pairs with Smallest Sums#17
ryoooooory merged 15 commits intomainfrom
task/373

Conversation

@ryoooooory
Copy link
Copy Markdown
Owner

int index1 = currentMinPair[1];
int index2 = currentMinPair[2];
kSmallestPairs.add(List.of(nums1[index1], nums2[index2]));
if (index1 + 1 < length1 && !visited.contains(new Pair<Integer, Integer>(index1 + 1, index2))) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(index1 + 1, index2)の要素を入れるのは index2が0のときのみ、とすると重複が発生せずvisitedが不要になりそうです。

            if (index2 == 0 && index1 + 1 < length1) {

visited.add(new Pair<Integer, Integer>(0, 0));

while(kSmallestPairs.size() < k) {
int[] currentMinPair = minSums.poll();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

currentMinPairといいつつ実際の要素数が3つなのは少し気になりました。
良い名前思いつかないんですがtotalAndIndexesとかですかね...

SumPair currentMinPair = minSortedPairs.poll();
int index1 = currentMinPair.indice.getKey();
int index2 = currentMinPair.indice.getValue();
kSmallestPairs.add(List.of(currentMinPair.elements.getKey(), currentMinPair.elements.getValue()));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

List.of(currentMinPair.elements.getKey(), currentMinPair.elements.getValue())

これ SumPair のメソッドにしてもいいんじゃないですか。

ほかも色々とメンバーにしてもいいかもしれませんね。


class SumPair {
int sum;
Pair<Integer, Integer> elements;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

elements ではなく、nums1, nums2 への参照を持つのも一つでしょうか。

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.

indexだけでことたりそうかなともおもいましたので、シンプルに合計値とIndexだけをもつようにしました 

minSortedPairs.add(new SumPair(nums1, index1, nums2, index2 + 1));
visited.add(new Pair<Integer, Integer>(index1, index2 + 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.

ここのところ、素直に日本語で表現すると、

  • PriorityQueue から取り出す
  • 答えに追加する
  • 右隣を作り、必要ならば PriorityQueue に追加する
  • 下隣を作り、必要ならば PriorityQueue に追加する
    くらいに書けると思うんですよ。ならば、そのようなメソッドを用意してもいいんじゃないでしょうか。

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.

ありがとうございます!共通処理の部分は別関数に切り出そうとおもいます

Copy link
Copy Markdown
Owner Author

@ryoooooory ryoooooory left a comment

Choose a reason for hiding this comment

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

コメントについて改修していみました!
こちらマージは行いますが、気になった点などあればどんどんコメントいただければとおもいます!

minSortedPairs.add(new SumPair(nums1, index1, nums2, index2 + 1));
visited.add(new Pair<Integer, Integer>(index1, index2 + 1));
}
}
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.

ありがとうございます!共通処理の部分は別関数に切り出そうとおもいます


class SumPair {
int sum;
Pair<Integer, Integer> elements;
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.

indexだけでことたりそうかなともおもいましたので、シンプルに合計値とIndexだけをもつようにしました 

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.

3 participants