From edaa491991d869c299708ecaa841ddb9dbcbe658 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Mon, 5 May 2025 17:09:06 +0900 Subject: [PATCH 1/5] feat : #17 add the empty markdown file for creating a PR --- hash-map/two-sum/answer.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 hash-map/two-sum/answer.md diff --git a/hash-map/two-sum/answer.md b/hash-map/two-sum/answer.md new file mode 100644 index 0000000..e69de29 From ec58f6abac5ba96029dfc76a7a5667bc09763748 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Mon, 5 May 2025 17:27:32 +0900 Subject: [PATCH 2/5] feat : #17 add STEP1/STEP2/STEP3 --- hash-map/two-sum/answer.md | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/hash-map/two-sum/answer.md b/hash-map/two-sum/answer.md index e69de29..66bb1ca 100644 --- a/hash-map/two-sum/answer.md +++ b/hash-map/two-sum/answer.md @@ -0,0 +1,68 @@ +# 1. Two Sum + +## STEP1 + +* 全てのnumに対して、 + * pairがいるかをチェックし、いた場合には終了。 + * HashMapにnumを追加 + +```javascript +const twoSum = function(nums, target) { + const num_to_index_table = new Map() + for (let i=0; i Date: Mon, 5 May 2025 23:06:59 +0900 Subject: [PATCH 3/5] feat : #17 add two more solutions and read three PRs --- hash-map/two-sum/answer.md | 102 ++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/hash-map/two-sum/answer.md b/hash-map/two-sum/answer.md index 66bb1ca..eca4255 100644 --- a/hash-map/two-sum/answer.md +++ b/hash-map/two-sum/answer.md @@ -2,10 +2,14 @@ ## STEP1 +### 発想 + * 全てのnumに対して、 * pairがいるかをチェックし、いた場合には終了。 * HashMapにnumを追加 +* 最初、pairのチェックよりも、mapへの追加を先に行なってしまい、[3,3]のケースを考慮できておらず、エラーとなってしまった。 + ```javascript const twoSum = function(nums, target) { const num_to_index_table = new Map() @@ -24,7 +28,7 @@ const twoSum = function(nums, target) { ## STEP2 -* 特に修正要素が見つからなかった. +* 添え字を、iではなく、idxに変更。 ```javascript const twoSum = function(nums, target) { @@ -62,7 +66,103 @@ const twoSum = function(nums, target) { ## 感想 +* Javascriptの標準エラーを今まで調べたことがなかった。 + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#error_types + * PythonのValueError(正しい型だが適切でない値を持つ引数を受け取ったとき)に相当するエラーを探したが、Javascriptでそれに相当するエラーはなさそうだった。 + +* twoポインターを解く方法 (`*2`) も、シンプルで個人的には好き。 + +### 他の人のPRを読んで + +* olsen-blueのコード + * PR: https://github.com/olsen-blue/Arai60/pull/11/ + * 回答が複数だった場合には、どうなるか? + * HashTableを使う方法は、先頭の2つをとる。 + * TwoPointerを使う方法だったら、先頭と最後になる。 + * pythonのcollectionモジュールのdefaultDictは,dictと違って存在しないkeyが呼ばれた際に、 + keyに対してdefault valueを値とするobjectが挿入され、default valueが返却される。 + * `If default_factory is not None, it is called without arguments to provide a default value for the given key, this value is inserted in the dictionary for the key, and returned.` + +* Ryotaro25のコード + * PR: https://github.com/Ryotaro25/leetcode_first60/pull/12 + * C++において、mapは赤黒木でできている。 + +* hayash-ayのコード + * PR: https://github.com/hayashi-ay/leetcode/pull/14/ + * `find_num`や`find_index` の変数名に違和感がある。`find`ではなく`found`の方が良いが、 + `found`でもわかりづらい。もう片方という意味を持つ変数名の方が、意図が伝わると思う。 + Anthropic Claudeに質問したところ、 `corresponding`, `twin`, `complementary`, `partner` とかが考えらるとのこと。 + * `num_idx_map` ではなく、 `num_to_idx`とした方が良い。何がkeyで、何がvalueかが明確になるため。 + ### コメント集を読んで +* 異常な入力について考える癖が出てきた。 + * ペアが見つからなかった時 + * ペアが複数見つかった + ## その他の解法 +* (`*1`) 総当たりをする方法 (時間計算量 N^2) + +```javascript +const twoSum = function(nums, target) { + for (let i = 0; i < nums.length - 1; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + return [i, j] + } + } + } + return new Error("pair is not found") +}; +``` + +* (`*2`) ソートをした後に、2つのポインターで実装する方法 + * 時間計算量がO(N logN)になる。配列をソートするため。 + +```javascript +const twoSum = function(nums, target) { + // ソートをしてしまうと、元々のインデックスが保存されないため、 + // 新しい配列を用意する。 + const nums_with_index = nums.map((num, i) => { + return { + value: num, + index: i, + } + }) + nums_with_index.sort((a, b) => a.value - b.value) + let left_idx = 0 + let right_idx = nums.length - 1 + while (left_idx < right_idx) { + const left = nums_with_index[left_idx] + const right = nums_with_index[right_idx] + if (left.value + right.value === target) { + return [left.index, right.index] + } + if (left.value + right.value < target) { + ++left_idx + continue + } + --right_idx + } + return new Error("pair is not found") +}; +``` + +* mapをもっと綺麗にかける。 + +* 変更前 + +```javascript + const nums_with_index = nums.map((num, i) => { + return { + value: num, + index: i, + } + }) +``` + +* 変更後 +```javascript + const nums_with_index = nums.map((value, index) => ({ value, index }) +``` From 987ca9d6306f9d74accb79a0c2eda2e33a24aeb5 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Mon, 5 May 2025 23:15:32 +0900 Subject: [PATCH 4/5] feat : #17 add the impression --- hash-map/two-sum/answer.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hash-map/two-sum/answer.md b/hash-map/two-sum/answer.md index eca4255..26c23e6 100644 --- a/hash-map/two-sum/answer.md +++ b/hash-map/two-sum/answer.md @@ -100,6 +100,13 @@ const twoSum = function(nums, target) { * ペアが見つからなかった時 * ペアが複数見つかった +* IF文に対する感覚を持てていなかった。 + * if の中のほうが、普通ではない、異常な、変わったことが起きて欲しいという感覚 + * 参考: https://discord.com/channels/1084280443945353267/1201211204547383386/1207251531041210408 + +* 目的ではなく、どう使うかを表す変数名にする。 + * 「食卓」「会議室」など使用目的を変数名に使うと、どのように使われるかのパズルを解かないといけない。 + ## その他の解法 * (`*1`) 総当たりをする方法 (時間計算量 N^2) From 402afc125c54bc677cbda32a2f19998dda73b728 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Mon, 5 May 2025 23:54:56 +0900 Subject: [PATCH 5/5] feat : #15 add the review comment with new codebase --- hash-map/two-sum/answer.md | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/hash-map/two-sum/answer.md b/hash-map/two-sum/answer.md index 26c23e6..878f5af 100644 --- a/hash-map/two-sum/answer.md +++ b/hash-map/two-sum/answer.md @@ -173,3 +173,49 @@ const twoSum = function(nums, target) { ```javascript const nums_with_index = nums.map((value, index) => ({ value, index }) ``` + +## レビューを踏まえて + +### レビュー(1) エラーのハンドリングについて + +* 変更前 + +```javascript +const twoSum = function(nums, target) { + const num_to_index = new Map() + for (const idx_str in nums) { + const idx = Number(idx_str) + const num = nums[idx] + const twin_num = target - num + if (num_to_index.has(twin_num)) { + const twin_idx = num_to_index.get(twin_num) + return [idx, twin_idx] + } + num_to_index.set(num, idx) + } + return new Error("pair is not found") +}; +``` + +* 実行を中断したいのであれば、`return new Exception`ではなく、`throw new Exception`をすべきというコメントを受けた + +* 変更後 + +```javascript +const twoSum = function(nums, target) { + const num_to_index = new Map() + for (const idx_str in nums) { + const idx = Number(idx_str) + const num = nums[idx] + const twin_num = target - num + if (num_to_index.has(twin_num)) { + const twin_idx = num_to_index.get(twin_num) + return [idx, twin_idx] + } + num_to_index.set(num, idx) + } + // UPDATED + throw new Error("pair is not found") +}; +``` +