-
Notifications
You must be signed in to change notification settings - Fork 0
Create 373. Find K Pairs with Smallest Sums.md #10
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # 1st | ||
| - 問題: [373. Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/) | ||
| - コメント集: [373. Find K Pairs with Smallest Sums](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.527w0lse8gbd) | ||
| - 気になったコメント | ||
| - [コメント1](https://discord.com/channels/1084280443945353267/1201211204547383386/1206515949579145216) | ||
| - 問題設定では必ずkはある想定なんでしたっけ? --> なるほどなと思う、k, num1,num2 がそれぞれ条件を満たしていないときに IllegalArgumentException を投げるように変更 | ||
| - [コメント2](https://discord.com/channels/1084280443945353267/1231966485610758196/1268091954537959437) | ||
| - なるほど、、そういう[方針](https://github.com/TORUS0818/leetcode/pull/12/changes)で皆さんといているんですね | ||
| - 最初に全然思いつかなかったです | ||
| - 条件 | ||
| - `1 <= nums1.length, nums2.length <= 10^5` | ||
| - `-10^9 <= nums1[i], nums2[i] <= 10^9` | ||
| - `nums1 and nums2 both are sorted in non-decreasing order.` | ||
| - `1 <= k <= 10^4` | ||
| - `k <= nums1.length * nums2.length` | ||
|
|
||
| - 方針 | ||
| - 二重ループを回し切ったら`10^10`で時間的に間に合わない | ||
| - 問題文に `nums1 and nums2 sorted in non-decreasing-order` と、太字で書いてあるのでこれを有効に使えば良さそう | ||
| - どちらかの配列を固定して、走査していく方針となるはず | ||
|
|
||
| ## 1st | ||
| - 30分 | ||
| - nums1, nums2 の配列サイズ以上の k が与えられた時を0から考えつくのは初見は難しい気がする、これは答えみないとわからんよ。。 | ||
| - 勘違いして解いてしまったけど備忘録として残しておく | ||
| ```java | ||
| class Solution { | ||
| public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| PriorityQueue<int[]> smallest = new PriorityQueue<>((a,b) -> a[0] - b[0]); | ||
| int counter = 0; | ||
|
|
||
| for (int num1 : nums1) { | ||
| if (counter == k) { | ||
| break; | ||
| } | ||
| for (int num2 : nums2) { | ||
| smallest.add(new int[]{num1 + num2, num1, num2}); | ||
| } | ||
| int[] temp = smallest.poll(); | ||
| List<Integer> pair = new ArrayList<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List.of() を使うとシンプルに書けると思いました。 result.add(List.of(temp[1], temp[2])); |
||
| pair.add(temp[1]); | ||
| pair.add(temp[2]); | ||
| result.add(pair); | ||
| counter++; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| - 答えを見た | ||
| - 時間計算量: nums1.length = n とすると、`O((n + k) log n)` | ||
| - 空間計算量: nums1.length = n とすると、`O(n + k)` | ||
| ```java | ||
| class Solution { | ||
| public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
|
|
||
| PriorityQueue<int[]> smallestQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); | ||
|
|
||
| for (int i = 0; i < nums1.length; i++) { | ||
| smallestQueue.add(new int[]{nums1[i] + nums2[0], i, 0}); | ||
| } | ||
|
|
||
| while (result.size() < k && !smallestQueue.isEmpty()) { | ||
| int[] smallest = smallestQueue.poll(); | ||
| int i = smallest[1]; | ||
| int j = smallest[2]; | ||
|
|
||
| List<Integer> pair = new ArrayList<>(); | ||
| pair.add(nums1[i]); | ||
| pair.add(nums2[j]); | ||
| result.add(pair); | ||
|
|
||
| // ここが肝. i = 0, j = 0 が1周目のループで確定していて、i = 1 ~ i.length, j = 0 しか smallestQueue には入っていない | ||
| // 次の最小値のペアとして考えられるのは i = 0, j = 1 なので、これを smallestQueue 保存する | ||
| // 2周目以降がちょっと追いづらくて、poll() された要素の i, j + 1 を次の候補とするので、書き出したケースによっては意味不明な当たり方をしているように見えてしまい、最初の理解でつまづきがち | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自分も理解があやふやな気がしたので、スライドを作ってみました。もしよければ見てみてください。 |
||
| if (j + 1 < nums2.length) { | ||
| smallestQueue.add(new int[]{nums1[i] + nums2[j + 1], i, j + 1}); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 2nd | ||
| - 読みやすく整形 | ||
| ```java | ||
| class Solution { | ||
| public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) { | ||
| if (k <= 0) { | ||
| throw new IllegalArgumentException("kには自然数を指定してください"); | ||
| } | ||
| if (nums1.length == 0 || nums2.length == 0) { | ||
| throw new IllegalArgumentException("サイズ1以上の配列を渡してください"); | ||
| } | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| PriorityQueue<int[]> indexPairQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 少し長くなりますがSumAnd*でもいいかもしれません。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whileのなかではそんな感じにしていますね。 |
||
|
|
||
| for (int i = 0; i < nums1.length; i++) { | ||
| indexPairQueue.add(new int[]{nums1[i] + nums2[0], i, 0}); | ||
| } | ||
|
|
||
| while (result.size() < k && !indexPairQueue.isEmpty()) { | ||
| int[] sumAndIndices = indexPairQueue.poll(); | ||
| int i = sumAndIndices[1]; | ||
| int j = sumAndIndices[2]; | ||
|
|
||
| List<Integer> pair = new ArrayList<>(); | ||
| pair.add(nums1[i]); | ||
| pair.add(nums2[j]); | ||
| result.add(pair); | ||
|
|
||
| if (j + 1 < nums2.length) { | ||
| indexPairQueue.add(new int[]{nums1[i] + nums2[j + 1], i, j + 1}); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 3rd | ||
| - 3回書き直し済 | ||
| ```java | ||
| class Solution { | ||
| public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) { | ||
| if (k <= 0) { | ||
| throw new IllegalArgumentException("kには自然数を指定してください"); | ||
| } | ||
| if (nums1.length == 0 || nums2.length == 0) { | ||
| throw new IllegalArgumentException("サイズ1以上の配列を渡してください"); | ||
| } | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| PriorityQueue<int[]> indexPairQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); | ||
|
|
||
| for (int i = 0; i < nums1.length; i++) { | ||
| indexPairQueue.add(new int[]{nums1[i] + nums2[0], i, 0}); | ||
| } | ||
|
|
||
| while (result.size() < k && !indexPairQueue.isEmpty()) { | ||
| int[] sumAndIndices = indexPairQueue.poll(); | ||
| int i = sumAndIndices[1]; | ||
| int j = sumAndIndices[2]; | ||
|
|
||
| List<Integer> pair = new ArrayList<>(); | ||
| pair.add(nums1[i]); | ||
| pair.add(nums2[j]); | ||
| result.add(pair); | ||
|
|
||
| if (j + 1 < nums2.length) { | ||
| indexPairQueue.add(new int[]{nums1[i] + nums2[j + 1], i, j + 1}); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| ``` | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これじゃ解けないのだろうか?と疑問に思ったのですが、smallestにすべて追加し終わっても取り出しが間に合っていないのだと思います。
つまり、最大k = nums1の長さの組み合わせまでしか出力できないのだと思います。LeetCodeで確認したところ、そうなっていました。