From 1bbc93bc30039105fa84c22ebde87ad4686b17cf Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 2 May 2024 01:35:26 +0900 Subject: [PATCH 1/9] phase1.py --- arai60/permutations/phase1.py | 14 +++++++++ arai60/permutations/phase2.py | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 arai60/permutations/phase1.py create mode 100644 arai60/permutations/phase2.py diff --git a/arai60/permutations/phase1.py b/arai60/permutations/phase1.py new file mode 100644 index 0000000..5403388 --- /dev/null +++ b/arai60/permutations/phase1.py @@ -0,0 +1,14 @@ +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + if len(nums) == 0: + return [[]] + + adding_number = nums.pop() + added_permutations = self.permute(nums) # permutations without adding_number + permutations: List[List[int]] = [] # all the pattern of the permutations of nums + for added_permutation in added_permutations: + for i in range(len(added_permutation)+1): + permutations.append(added_permutation[:i] + [adding_number] + added_permutation[i:]) + # insert adding_number as i-th number of added_permutations + + return permutations \ No newline at end of file diff --git a/arai60/permutations/phase2.py b/arai60/permutations/phase2.py new file mode 100644 index 0000000..91e2e10 --- /dev/null +++ b/arai60/permutations/phase2.py @@ -0,0 +1,53 @@ +""" +Reference +shining-aiさん: https://github.com/shining-ai/leetcode/pull/50/files itertoolsでの解答と再帰によるbacktrackを見ました。再帰でのbacktrackを練習してみます。 +hayashi-ayさん: https://github.com/hayashi-ay/leetcode/pull/57/files + +今回は全ての数が等しい前提のもとでコードを書いたが, next_permutationを用いた方が重複がある場合にも対応できるのではないかと思った。 +Phase1で書いたものの時間計算量はO(Σ_{0->n}k!)という悲惨なものに, これは書いてはいけない。 +要素数10で比較してみた結果 +itertoolsを使った場合: 0.5979518890380859 +phase1解法: 2.563912868499756 +再帰でbacktrackをする方法: 0.0003998279571533203 +とにかく似たような書いたように見えていても本当にまずいことをしていることがわかった。再帰を書くときはnaiveなフィボナッチ実装のように再帰が積み上がってないかを確認 +""" + +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + return list(itertools.permutations(nums, len(nums))) + +# phase1で書いたもの, 時間計算量が非常に高い, この回答はまずい気がする +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + if len(nums) == 0: + return [[]] + + adding_number = nums.pop() + added_permutations = self.permute(nums) # all the permutations of nums without adding_number + permutations: List[List[int]] = [] # all the permutations of nums + for added_permutation in added_permutations: + for i in range(len(added_permutation)+1): + permutations.append(added_permutation[:i] + [adding_number] + added_permutation[i:]) + # insert adding_number as i-th number of added_permutations + + return permutations + +# 再帰でbacktrack +# イメージ的には, ある要素を加える, その要素を加える前の状態に戻るを繰り返す感じだろうか + +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + def make_permutations(current): + if len(current) == len(nums): + permutations.append(current[:]) + return + for num in nums: + if num in current: + continue + current.append(num) + make_permutations(current) + current.pop() + + permutations = [] + make_permutations([]) + return permutations From 473fc6f9a14ed1bb86a4252f0702f8f239018089 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 2 May 2024 01:35:39 +0900 Subject: [PATCH 2/9] phase2.py --- arai60/permutations/phase2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arai60/permutations/phase2.py b/arai60/permutations/phase2.py index 91e2e10..93df19f 100644 --- a/arai60/permutations/phase2.py +++ b/arai60/permutations/phase2.py @@ -8,7 +8,7 @@ 要素数10で比較してみた結果 itertoolsを使った場合: 0.5979518890380859 phase1解法: 2.563912868499756 -再帰でbacktrackをする方法: 0.0003998279571533203 +再帰でbacktrackをする方法: 0.0003998279571533203 とにかく似たような書いたように見えていても本当にまずいことをしていることがわかった。再帰を書くときはnaiveなフィボナッチ実装のように再帰が積み上がってないかを確認 """ From ecb2bbb91d5aec7849e62174683e485f4e4c8bbe Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 2 May 2024 01:35:48 +0900 Subject: [PATCH 3/9] phase3.py --- arai60/permutations/phase3.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 arai60/permutations/phase3.py diff --git a/arai60/permutations/phase3.py b/arai60/permutations/phase3.py new file mode 100644 index 0000000..b661309 --- /dev/null +++ b/arai60/permutations/phase3.py @@ -0,0 +1,16 @@ +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + all_permutations: List[List[int]] = [] + def make_permutations(candidates: List[int]): + if len(candidates) == len(nums): + all_permutations.append(candidates.copy()) + return + for num in nums: + if num in candidates: + continue + candidates.append(num) + make_permutations(candidates) + candidates.pop() # backtrack + + make_permutations([]) + return all_permutations \ No newline at end of file From eabc5048ddf22df6755e6e7b09661754203c08f5 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 21 May 2024 17:58:16 +0900 Subject: [PATCH 4/9] phase1 --- arai60/remove_duplicates_from_sorted_list/phase1.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 arai60/remove_duplicates_from_sorted_list/phase1.py diff --git a/arai60/remove_duplicates_from_sorted_list/phase1.py b/arai60/remove_duplicates_from_sorted_list/phase1.py new file mode 100644 index 0000000..96583f4 --- /dev/null +++ b/arai60/remove_duplicates_from_sorted_list/phase1.py @@ -0,0 +1,13 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy_head = head + while head: + while head.next and head.val == head.next.val: + head.next = head.next.next + head = head.next + return dummy_head \ No newline at end of file From 24947a9624f0a9e7f3a1be41b965ef5544773f6c Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 21 May 2024 17:58:25 +0900 Subject: [PATCH 5/9] phase2 --- .../phase2.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 arai60/remove_duplicates_from_sorted_list/phase2.py diff --git a/arai60/remove_duplicates_from_sorted_list/phase2.py b/arai60/remove_duplicates_from_sorted_list/phase2.py new file mode 100644 index 0000000..149bfc9 --- /dev/null +++ b/arai60/remove_duplicates_from_sorted_list/phase2.py @@ -0,0 +1,33 @@ +""" +Reference: +Ryotaro25: https://github.com/Ryotaro25/leetcode_first60/pull/3/files phase1の新しくListNodeを制作して末端の値と等しくなければ飛ばすコードを真似して書いてみた +kagetora0924: https://github.com/kagetora0924/leetcode-grind/pull/6/files 自分のphase1の方が状態管理する変数が少なくて頭のメモリをあまり消費しなくて良い気がした +fhiyo: https://github.com/fhiyo/leetcode/pull/3/files 重複ノード削除を関数にしていた。自分のコードにもせめてコメントぐらいは書くべきだと思い追記 +""" + +# Ryotaroさんのコードを参考に制作, 新しくListNodeを作るやり方 +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + if head is None: + return head + + dummy_head = ListNode(-1) + dummy_head.next = ListNode(head.val) + sentinel = dummy_head.next + head = head.next + while head: + if head.val != sentinel.val: + sentinel.next = ListNode(head.val) + sentinel = sentinel.next + head = head.next + return dummy_head.next + +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy_head = head + while head: + while head.next and head.val == head.next.val: + # reconnect listnodes in order to delete duplicate ones. + head.next = head.next.next + head = head.next + return dummy_head \ No newline at end of file From 32a8ab74ffe05f126df7f394a79dc43de20feca8 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 21 May 2024 17:58:33 +0900 Subject: [PATCH 6/9] phase3 --- arai60/remove_duplicates_from_sorted_list/phase3.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 arai60/remove_duplicates_from_sorted_list/phase3.py diff --git a/arai60/remove_duplicates_from_sorted_list/phase3.py b/arai60/remove_duplicates_from_sorted_list/phase3.py new file mode 100644 index 0000000..fe0f162 --- /dev/null +++ b/arai60/remove_duplicates_from_sorted_list/phase3.py @@ -0,0 +1,9 @@ +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy_head = head + while head: + while head.next and head.val == head.next.val: + # recconect listnodes in order to delete duplicates + head.next = head.next.next + head = head.next + return dummy_head \ No newline at end of file From 308249e84b1573ba90189ca277ae77af4edad695 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 21 May 2024 18:01:36 +0900 Subject: [PATCH 7/9] Delete unnecessary files --- arai60/permutations/phase1.py | 14 --------- arai60/permutations/phase2.py | 53 ----------------------------------- arai60/permutations/phase3.py | 16 ----------- 3 files changed, 83 deletions(-) delete mode 100644 arai60/permutations/phase1.py delete mode 100644 arai60/permutations/phase2.py delete mode 100644 arai60/permutations/phase3.py diff --git a/arai60/permutations/phase1.py b/arai60/permutations/phase1.py deleted file mode 100644 index 5403388..0000000 --- a/arai60/permutations/phase1.py +++ /dev/null @@ -1,14 +0,0 @@ -class Solution: - def permute(self, nums: List[int]) -> List[List[int]]: - if len(nums) == 0: - return [[]] - - adding_number = nums.pop() - added_permutations = self.permute(nums) # permutations without adding_number - permutations: List[List[int]] = [] # all the pattern of the permutations of nums - for added_permutation in added_permutations: - for i in range(len(added_permutation)+1): - permutations.append(added_permutation[:i] + [adding_number] + added_permutation[i:]) - # insert adding_number as i-th number of added_permutations - - return permutations \ No newline at end of file diff --git a/arai60/permutations/phase2.py b/arai60/permutations/phase2.py deleted file mode 100644 index 93df19f..0000000 --- a/arai60/permutations/phase2.py +++ /dev/null @@ -1,53 +0,0 @@ -""" -Reference -shining-aiさん: https://github.com/shining-ai/leetcode/pull/50/files itertoolsでの解答と再帰によるbacktrackを見ました。再帰でのbacktrackを練習してみます。 -hayashi-ayさん: https://github.com/hayashi-ay/leetcode/pull/57/files - -今回は全ての数が等しい前提のもとでコードを書いたが, next_permutationを用いた方が重複がある場合にも対応できるのではないかと思った。 -Phase1で書いたものの時間計算量はO(Σ_{0->n}k!)という悲惨なものに, これは書いてはいけない。 -要素数10で比較してみた結果 -itertoolsを使った場合: 0.5979518890380859 -phase1解法: 2.563912868499756 -再帰でbacktrackをする方法: 0.0003998279571533203 -とにかく似たような書いたように見えていても本当にまずいことをしていることがわかった。再帰を書くときはnaiveなフィボナッチ実装のように再帰が積み上がってないかを確認 -""" - -class Solution: - def permute(self, nums: List[int]) -> List[List[int]]: - return list(itertools.permutations(nums, len(nums))) - -# phase1で書いたもの, 時間計算量が非常に高い, この回答はまずい気がする -class Solution: - def permute(self, nums: List[int]) -> List[List[int]]: - if len(nums) == 0: - return [[]] - - adding_number = nums.pop() - added_permutations = self.permute(nums) # all the permutations of nums without adding_number - permutations: List[List[int]] = [] # all the permutations of nums - for added_permutation in added_permutations: - for i in range(len(added_permutation)+1): - permutations.append(added_permutation[:i] + [adding_number] + added_permutation[i:]) - # insert adding_number as i-th number of added_permutations - - return permutations - -# 再帰でbacktrack -# イメージ的には, ある要素を加える, その要素を加える前の状態に戻るを繰り返す感じだろうか - -class Solution: - def permute(self, nums: List[int]) -> List[List[int]]: - def make_permutations(current): - if len(current) == len(nums): - permutations.append(current[:]) - return - for num in nums: - if num in current: - continue - current.append(num) - make_permutations(current) - current.pop() - - permutations = [] - make_permutations([]) - return permutations diff --git a/arai60/permutations/phase3.py b/arai60/permutations/phase3.py deleted file mode 100644 index b661309..0000000 --- a/arai60/permutations/phase3.py +++ /dev/null @@ -1,16 +0,0 @@ -class Solution: - def permute(self, nums: List[int]) -> List[List[int]]: - all_permutations: List[List[int]] = [] - def make_permutations(candidates: List[int]): - if len(candidates) == len(nums): - all_permutations.append(candidates.copy()) - return - for num in nums: - if num in candidates: - continue - candidates.append(num) - make_permutations(candidates) - candidates.pop() # backtrack - - make_permutations([]) - return all_permutations \ No newline at end of file From 01d90ac53f378693adce2cda70d393825136cb02 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 21 May 2024 23:22:20 +0900 Subject: [PATCH 8/9] =?UTF-8?q?=E6=94=B9=E8=A1=8C=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arai60/remove_duplicates_from_sorted_list/phase1.py | 4 +++- arai60/remove_duplicates_from_sorted_list/phase2.py | 4 +++- arai60/remove_duplicates_from_sorted_list/phase3.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/arai60/remove_duplicates_from_sorted_list/phase1.py b/arai60/remove_duplicates_from_sorted_list/phase1.py index 96583f4..e4a6624 100644 --- a/arai60/remove_duplicates_from_sorted_list/phase1.py +++ b/arai60/remove_duplicates_from_sorted_list/phase1.py @@ -10,4 +10,6 @@ def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: while head.next and head.val == head.next.val: head.next = head.next.next head = head.next - return dummy_head \ No newline at end of file + return dummy_head + + \ No newline at end of file diff --git a/arai60/remove_duplicates_from_sorted_list/phase2.py b/arai60/remove_duplicates_from_sorted_list/phase2.py index 149bfc9..7ec0094 100644 --- a/arai60/remove_duplicates_from_sorted_list/phase2.py +++ b/arai60/remove_duplicates_from_sorted_list/phase2.py @@ -30,4 +30,6 @@ def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: # reconnect listnodes in order to delete duplicate ones. head.next = head.next.next head = head.next - return dummy_head \ No newline at end of file + return dummy_head + + \ No newline at end of file diff --git a/arai60/remove_duplicates_from_sorted_list/phase3.py b/arai60/remove_duplicates_from_sorted_list/phase3.py index fe0f162..f34d41c 100644 --- a/arai60/remove_duplicates_from_sorted_list/phase3.py +++ b/arai60/remove_duplicates_from_sorted_list/phase3.py @@ -6,4 +6,6 @@ def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: # recconect listnodes in order to delete duplicates head.next = head.next.next head = head.next - return dummy_head \ No newline at end of file + return dummy_head + + \ No newline at end of file From 6c474c23df76f88459aba726edcb31ec1ad379e7 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 21 May 2024 23:22:54 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=E5=8B=95=E3=81=8B=E3=81=99=E3=82=82?= =?UTF-8?q?=E3=81=AE=E3=82=92node=E3=81=AB=E5=90=8D=E5=90=8D=E5=A4=89?= =?UTF-8?q?=E6=9B=B4,=20=E5=86=8D=E5=B8=B0=E3=83=90=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E3=82=82=E8=BF=BD=E8=A8=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../phase4.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 arai60/remove_duplicates_from_sorted_list/phase4.py diff --git a/arai60/remove_duplicates_from_sorted_list/phase4.py b/arai60/remove_duplicates_from_sorted_list/phase4.py new file mode 100644 index 0000000..962acc1 --- /dev/null +++ b/arai60/remove_duplicates_from_sorted_list/phase4.py @@ -0,0 +1,27 @@ +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + node = head + while node: + while node.next and node.val == node.next.val: + node.next = node.next.next + node = node.next + return head + +# 再帰version +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + node = head + def deleteDuplicatesNodes(node: Optional[ListNode]) -> None: + # nodeより先の重複したノードを返す + if node == None: + return None + if node.next and node.val == node.next.val: + node.next = node.next.next + return deleteDuplicatesNodes(node) + elif node.next is None: + return deleteDuplicatesNodes(None) + else: + return deleteDuplicatesNodes(node.next) + deleteDuplicatesNodes(node) + return head +