From c901cc9d0fce210f930a7498099481aabbbfbb2a Mon Sep 17 00:00:00 2001 From: aiueoriku <112373068+aiueoriku@users.noreply.github.com> Date: Tue, 20 May 2025 12:11:51 +0900 Subject: [PATCH 1/4] Create TwySum.md --- TwoSum/TwySum.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 TwoSum/TwySum.md diff --git a/TwoSum/TwySum.md b/TwoSum/TwySum.md new file mode 100644 index 0000000..b42fe14 --- /dev/null +++ b/TwoSum/TwySum.md @@ -0,0 +1,17 @@ +"""python +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + for i in range(len(nums)): + for j in range(i+1, len(nums)): + if nums[i]+nums[j]==target: + return [i,j] + break +""" + +# 感想 +forループを2つ回して愚直に行ったが,より効率の良いアルゴリズムがありそう. From 28e9a53237501707fefd7e15b30c6543b34d6051 Mon Sep 17 00:00:00 2001 From: aiueoriku <112373068+aiueoriku@users.noreply.github.com> Date: Tue, 20 May 2025 12:38:59 +0900 Subject: [PATCH 2/4] Update and rename TwySum.md to TwoSum.md --- TwoSum/{TwySum.md => TwoSum.md} | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) rename TwoSum/{TwySum.md => TwoSum.md} (96%) diff --git a/TwoSum/TwySum.md b/TwoSum/TwoSum.md similarity index 96% rename from TwoSum/TwySum.md rename to TwoSum/TwoSum.md index b42fe14..6d55ff2 100644 --- a/TwoSum/TwySum.md +++ b/TwoSum/TwoSum.md @@ -1,4 +1,5 @@ -"""python +```python + class Solution(object): def twoSum(self, nums, target): """ @@ -11,7 +12,7 @@ class Solution(object): if nums[i]+nums[j]==target: return [i,j] break -""" +``` # 感想 forループを2つ回して愚直に行ったが,より効率の良いアルゴリズムがありそう. From 29422c97ab80ee4cdf2ab8354d09c22db17641f0 Mon Sep 17 00:00:00 2001 From: aiueoriku <112373068+aiueoriku@users.noreply.github.com> Date: Tue, 20 May 2025 15:21:32 +0900 Subject: [PATCH 3/4] do step1,2, and 3 --- TwoSum/TwoSum.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/TwoSum/TwoSum.md b/TwoSum/TwoSum.md index 6d55ff2..1199133 100644 --- a/TwoSum/TwoSum.md +++ b/TwoSum/TwoSum.md @@ -1,3 +1,6 @@ +# Step 1 +まずは愚直に実行 + ```python class Solution(object): @@ -13,6 +16,52 @@ class Solution(object): return [i,j] break ``` +実行時間は2271ms + + + +# Step 2 +PRや公式解答を参照. +解答を見るとhashmapという手法を発見.dictを用いることで高速に探索出来るらしい. +keyの操作に慣れていないので,デバッグしつつ実装 +```python +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + hashmap = {} + for i in range(len(nums)): + hashmap[nums[i]] = i + print(nums) # [2, 7, 11, 15] + print(hashmap) # {15: 3, 2: 0, 11: 2, 7: 1} + for i in range(len(nums)): + complement = target - nums[i] + if complement in hashmap and hashmap[complement] != i: # dict[key]=value + return [i, hashmap[complement]] + return [] +``` +実行時間は158ms.計算量を減らすことが出来た. -# 感想 -forループを2つ回して愚直に行ったが,より効率の良いアルゴリズムがありそう. +# Step 3 +何も見ないで実装 +```python +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + hashmap = {} + for i in range(len(nums)): + hashmap[nums[i]] = i + for i in range(len(nums)): + complement = target - nums[i] + if complement in hashmap and hashmap[complement]!=i: + return [i, hashmap[complement]] + return [] +``` +実行時間は5ms同じコードなのに実行時間が違う.他に見るべき指標があるかも. From c5367f19562f518b40585fbdd1d5f8920b1964e1 Mon Sep 17 00:00:00 2001 From: aiueoriku <112373068+aiueoriku@users.noreply.github.com> Date: Wed, 21 May 2025 09:22:27 +0900 Subject: [PATCH 4/4] add Step4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit いただいたアドバイスを元にStep4を追記 --- TwoSum/TwoSum.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/TwoSum/TwoSum.md b/TwoSum/TwoSum.md index 1199133..4cc1bc8 100644 --- a/TwoSum/TwoSum.md +++ b/TwoSum/TwoSum.md @@ -65,3 +65,32 @@ class Solution(object): return [] ``` 実行時間は5ms同じコードなのに実行時間が違う.他に見るべき指標があるかも. + +# Step 4 +いただいたアドバイスを元に実装 +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_to_index = {} + for i in range (len(nums)): + complement = target - nums[i] + if complement in num_to_index and num_to_index[complement]!=i: + return [i, num_to_index[complement]] + num_to_index[nums[i]] = i + return [] +``` +辞書の名前をnum_to_indexに変更して意味を分かりやすくした. +for文を1つにまとめた. + +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_to_index = {} + for i, num in enumerate(nums): + complement = target - num + if complement in num_to_index and num_to_index[complement]!=i: + return [num_to_index[complement], i] + num_to_index[num] = i + return [] +``` +enumerateでも書いてみた.個人的にはこちらのほうが好みかも.