From 94e29f20c543a3c3a3031bca695429822aeddd31 Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Thu, 2 May 2024 12:46:43 +0900 Subject: [PATCH 1/5] complete --- arai60/generate-parentheses/step1.py | 69 ++++++++++++++++++++++++++++ arai60/generate-parentheses/step2.py | 58 +++++++++++++++++++++++ arai60/generate-parentheses/step3.py | 25 ++++++++++ 3 files changed, 152 insertions(+) create mode 100644 arai60/generate-parentheses/step1.py create mode 100644 arai60/generate-parentheses/step2.py create mode 100644 arai60/generate-parentheses/step3.py diff --git a/arai60/generate-parentheses/step1.py b/arai60/generate-parentheses/step1.py new file mode 100644 index 0000000..e7e433a --- /dev/null +++ b/arai60/generate-parentheses/step1.py @@ -0,0 +1,69 @@ +""" +21m + +考察: + openかcloseかを入れる + 入ってるopenの数もカウントしておいて、それが正ならcloseも入れる +反省: + できれば返り値を処理する形で書きたかったが、書き方がわからなかった + popしなくてよいので、bfsでも楽に書けることまで考えればよかった。解き方を思いついても他の選択肢を軽く検討して、提示すべき + 解き方自体はすぐに思いついているのに、時間がかかるのをどうにかしたい。 + 変数名を考えるのは少し慣れてきたが、全体でなぜここまで遅くなってしまっているのかは + 自分でもよくわかっていない。エディタを使用せずにすべて直打ちしているのも影響している? + ただ、毎回ちゃんと反省をして演習量を積めば改善される気はしている。 + とりあえず変数名をすぐに思いつけるようにして、手戻りのないように実装できるようになろう。 +""" + +# dfs +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + answer = [] + def append_bracket(bracket_limit, bracket_stack): + if len(bracket_stack) == bracket_limit * 2: + return bracket_stack + + open_count = bracket_stack.count("(") + close_count = bracket_stack.count(")") + if open_count < bracket_limit: + new_bracket_stack = list(bracket_stack) + new_bracket_stack.append("(") + res = append_bracket(bracket_limit, new_bracket_stack) + if res: + answer.append("".join(res)) + if open_count - close_count > 0: + new_bracket_stack = list(bracket_stack) + new_bracket_stack.append(")") + res = append_bracket(bracket_limit, new_bracket_stack) + if res: + answer.append("".join(res)) + + append_bracket(n, []) + return answer + +# bfs +from collections import deque + +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + bracket_combinations = [] + queue = deque() + queue.append([]) + + while len(queue) > 0: + bracket_stack = queue.popleft() + if len(bracket_stack) == 2 * n: + bracket_combinations.append("".join(bracket_stack)) + continue + + open_count = bracket_stack.count("(") + close_count = bracket_stack.count(")") + if open_count < n: + new_bracket_stack = list(bracket_stack) + new_bracket_stack.append("(") + queue.append(new_bracket_stack) + if open_count - close_count > 0: + new_bracket_stack = list(bracket_stack) + new_bracket_stack.append(")") + queue.append(new_bracket_stack) + + return bracket_combinations diff --git a/arai60/generate-parentheses/step2.py b/arai60/generate-parentheses/step2.py new file mode 100644 index 0000000..21dbe71 --- /dev/null +++ b/arai60/generate-parentheses/step2.py @@ -0,0 +1,58 @@ +""" +条件が満たされたときにその場で追加すればよい。返り値で管理しようとしてたのに引っ張られて実装してしまった + +listじゃなく文字列で管理すれば簡潔には書けるが、再構築の可能性があるのがちょっと微妙 +leetcodeのsolutionsだとみんなopen_countとclose_countを引数にして、状態は文字列で管理してるんだな +初期値を["("]にするのはたしかに +""" + +#dfs listで管理 +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + bracket_combinations = [] + def append_bracket(bracket_limit, brackets): + if len(brackets) == bracket_limit * 2: + bracket_combinations.append("".join(brackets)) + return + + open_count = brackets.count("(") + close_count = brackets.count(")") + if open_count < bracket_limit: + new_brackets = list(brackets) + new_brackets.append("(") + append_bracket(bracket_limit, new_brackets) + if open_count > close_count: + new_brackets = list(brackets) + new_brackets.append(")") + append_bracket(bracket_limit, new_brackets) + + append_bracket(n, []) + return bracket_combinations + +#bfs +from collections import deque + +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + bracket_combinations = [] + processing_brackets = deque() + processing_brackets.append([]) + + while len(processing_brackets) > 0: + brackets = processing_brackets.popleft() + if len(brackets) == 2 * n: + bracket_combinations.append("".join(brackets)) + continue + + open_count = brackets.count("(") + close_count = brackets.count(")") + if open_count < n: + new_brackets = list(brackets) + new_brackets.append("(") + processing_brackets.append(new_brackets) + if open_count > close_count: + new_brackets = list(brackets) + new_brackets.append(")") + processing_brackets.append(new_brackets) + + return bracket_combinations diff --git a/arai60/generate-parentheses/step3.py b/arai60/generate-parentheses/step3.py new file mode 100644 index 0000000..ced37e7 --- /dev/null +++ b/arai60/generate-parentheses/step3.py @@ -0,0 +1,25 @@ +""" +4m50s +""" + +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + brackets_combinations = [] + def append_bracket(bracket_limit, brackets): + if len(brackets) == bracket_limit * 2: + brackets_combinations.append("".join(brackets)) + return + + open_count = brackets.count("(") + close_count = brackets.count(")") + if open_count < bracket_limit: + new_brackets = list(brackets) + new_brackets.append("(") + append_bracket(bracket_limit, new_brackets) + if open_count > close_count: + new_brackets = list(brackets) + new_brackets.append(")") + append_bracket(bracket_limit, new_brackets) + + append_bracket(n, []) + return brackets_combinations From 90c2fdb063bba09b09dc658f78cc0c23a8c106aa Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Thu, 2 May 2024 12:50:39 +0900 Subject: [PATCH 2/5] fix --- arai60/generate-parentheses/step2.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arai60/generate-parentheses/step2.py b/arai60/generate-parentheses/step2.py index 21dbe71..9173bd0 100644 --- a/arai60/generate-parentheses/step2.py +++ b/arai60/generate-parentheses/step2.py @@ -1,5 +1,8 @@ """ 条件が満たされたときにその場で追加すればよい。返り値で管理しようとしてたのに引っ張られて実装してしまった +answer→bracket_combinations +bracket_stack→brackets +queue→processing_brackets listじゃなく文字列で管理すれば簡潔には書けるが、再構築の可能性があるのがちょっと微妙 leetcodeのsolutionsだとみんなopen_countとclose_countを引数にして、状態は文字列で管理してるんだな From 07ca763a97db00b8634b49671e9405f6b2559e0e Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Sat, 4 May 2024 10:17:46 +0900 Subject: [PATCH 3/5] add step4 --- arai60/generate-parentheses/step4.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 arai60/generate-parentheses/step4.py diff --git a/arai60/generate-parentheses/step4.py b/arai60/generate-parentheses/step4.py new file mode 100644 index 0000000..215b606 --- /dev/null +++ b/arai60/generate-parentheses/step4.py @@ -0,0 +1,27 @@ +""" +nは定数だから引数に含めなくてよい +関数名がparenthesisだから変数名もそれに合わせたほうが良い +毎回open_countを数えると計算量がn倍増えるから引数で管理 +毎回list化すると計算量がn倍増えるからpopするようにする +""" + +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + brackets_combinations = [] + def append_bracket(parentheses, open_count): + if len(parentheses) == n * 2: + brackets_combinations.append("".join(parentheses)) + return + + close_count = len(parentheses) - open_count + if open_count < n: + parentheses.append("(") + append_bracket(parentheses, open_count+1) + parentheses.pop() + if open_count > close_count: + parentheses.append(")") + append_bracket(parentheses, open_count) + parentheses.pop() + + append_bracket([], 0) + return brackets_combinations From 548a0929c64a6637da770c4d3be4d54c2948a19f Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Sat, 4 May 2024 11:24:09 +0900 Subject: [PATCH 4/5] change brancket to parenthesis --- arai60/generate-parentheses/step4.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arai60/generate-parentheses/step4.py b/arai60/generate-parentheses/step4.py index 215b606..ae99ef0 100644 --- a/arai60/generate-parentheses/step4.py +++ b/arai60/generate-parentheses/step4.py @@ -7,21 +7,21 @@ class Solution: def generateParenthesis(self, n: int) -> List[str]: - brackets_combinations = [] - def append_bracket(parentheses, open_count): + parentheses_combinations = [] + def append_parenthesis(parentheses, open_count): if len(parentheses) == n * 2: - brackets_combinations.append("".join(parentheses)) + parentheses_combinations.append("".join(parentheses)) return close_count = len(parentheses) - open_count if open_count < n: parentheses.append("(") - append_bracket(parentheses, open_count+1) + append_parenthesis(parentheses, open_count + 1) parentheses.pop() if open_count > close_count: parentheses.append(")") - append_bracket(parentheses, open_count) + append_parenthesis(parentheses, open_count) parentheses.pop() - append_bracket([], 0) - return brackets_combinations + append_parenthesis([], 0) + return parentheses_combinations From d80dec9d731ec11e8c9fe8a133647efe8bc44fca Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Sat, 4 May 2024 12:00:42 +0900 Subject: [PATCH 5/5] delete parenthesis from arguments --- arai60/generate-parentheses/step4.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arai60/generate-parentheses/step4.py b/arai60/generate-parentheses/step4.py index ae99ef0..a097b6d 100644 --- a/arai60/generate-parentheses/step4.py +++ b/arai60/generate-parentheses/step4.py @@ -8,7 +8,8 @@ class Solution: def generateParenthesis(self, n: int) -> List[str]: parentheses_combinations = [] - def append_parenthesis(parentheses, open_count): + parentheses = [] + def append_parenthesis(open_count): if len(parentheses) == n * 2: parentheses_combinations.append("".join(parentheses)) return @@ -16,12 +17,12 @@ def append_parenthesis(parentheses, open_count): close_count = len(parentheses) - open_count if open_count < n: parentheses.append("(") - append_parenthesis(parentheses, open_count + 1) + append_parenthesis(open_count + 1) parentheses.pop() if open_count > close_count: parentheses.append(")") - append_parenthesis(parentheses, open_count) + append_parenthesis(open_count) parentheses.pop() - append_parenthesis([], 0) + append_parenthesis(0) return parentheses_combinations