Conversation
| 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))) { |
There was a problem hiding this comment.
(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(); |
There was a problem hiding this comment.
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())); |
There was a problem hiding this comment.
List.of(currentMinPair.elements.getKey(), currentMinPair.elements.getValue())これ SumPair のメソッドにしてもいいんじゃないですか。
ほかも色々とメンバーにしてもいいかもしれませんね。
|
|
||
| class SumPair { | ||
| int sum; | ||
| Pair<Integer, Integer> elements; |
There was a problem hiding this comment.
elements ではなく、nums1, nums2 への参照を持つのも一つでしょうか。
There was a problem hiding this comment.
indexだけでことたりそうかなともおもいましたので、シンプルに合計値とIndexだけをもつようにしました
| minSortedPairs.add(new SumPair(nums1, index1, nums2, index2 + 1)); | ||
| visited.add(new Pair<Integer, Integer>(index1, index2 + 1)); | ||
| } | ||
| } |
There was a problem hiding this comment.
ここのところ、素直に日本語で表現すると、
- PriorityQueue から取り出す
- 答えに追加する
- 右隣を作り、必要ならば PriorityQueue に追加する
- 下隣を作り、必要ならば PriorityQueue に追加する
くらいに書けると思うんですよ。ならば、そのようなメソッドを用意してもいいんじゃないでしょうか。
There was a problem hiding this comment.
ありがとうございます!共通処理の部分は別関数に切り出そうとおもいます
ryoooooory
left a comment
There was a problem hiding this comment.
コメントについて改修していみました!
こちらマージは行いますが、気になった点などあればどんどんコメントいただければとおもいます!
| minSortedPairs.add(new SumPair(nums1, index1, nums2, index2 + 1)); | ||
| visited.add(new Pair<Integer, Integer>(index1, index2 + 1)); | ||
| } | ||
| } |
There was a problem hiding this comment.
ありがとうございます!共通処理の部分は別関数に切り出そうとおもいます
|
|
||
| class SumPair { | ||
| int sum; | ||
| Pair<Integer, Integer> elements; |
There was a problem hiding this comment.
indexだけでことたりそうかなともおもいましたので、シンプルに合計値とIndexだけをもつようにしました
問題URL:https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/