Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions arai60/generate-parentheses/step1.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bracket_limitは変化しないので、append_bracketのパラメータに含める必要はないです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たしかに、定数として扱ってよいということですね。
ありがとうございます。
自分に欠けていた感覚でした。

if len(bracket_stack) == bracket_limit * 2:
return bracket_stack
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数名はgenerateParenthesisになっているので、bracketではなく、parenthesisに統一した方がいいでしょう。


open_count = bracket_stack.count("(")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

毎回カウントしているため、時間計算量が n 倍増えてしまっていると思います。 open_count を引数で引き回してはいかがでしょうか?また、 close_count は open_count と blacket_limit から計算できると思います。

個人的には変数名は num_opens にすると思います。 open_count でも良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。
本当ですね...修正します🙏

close_count = bracket_stack.count(")")
if open_count < bracket_limit:
new_bracket_stack = list(bracket_stack)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新しい list を毎回作っているため、ここでも時間計算量が n 倍増えてしまっているように思います。 append して、 append_bracket() から返ってきたときに pop() してはいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらもご指摘ありがとうございます。
修正します🙏

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
61 changes: 61 additions & 0 deletions arai60/generate-parentheses/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
条件が満たされたときにその場で追加すればよい。返り値で管理しようとしてたのに引っ張られて実装してしまった
answer→bracket_combinations
bracket_stack→brackets
queue→processing_brackets

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
25 changes: 25 additions & 0 deletions arai60/generate-parentheses/step3.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions arai60/generate-parentheses/step4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
nは定数だから引数に含めなくてよい
関数名がparenthesisだから変数名もそれに合わせたほうが良い
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まだbracketという単語使っていますよね。brackets_combinations, append_bracket

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ほんとですね...
ありがとうございます🙏

毎回open_countを数えると計算量がn倍増えるから引数で管理
毎回list化すると計算量がn倍増えるからpopするようにする
"""

class Solution:
def generateParenthesis(self, n: int) -> List[str]:
parentheses_combinations = []
parentheses = []
def append_parenthesis(open_count):
if len(parentheses) == n * 2:
parentheses_combinations.append("".join(parentheses))
return

close_count = len(parentheses) - open_count
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数に入れるのは肯定的です。場所は、必要となる場所の上くらいでもいいかも。

if open_count < n:
parentheses.append("(")
append_parenthesis(open_count + 1)
parentheses.pop()
if open_count > close_count:
parentheses.append(")")
append_parenthesis(open_count)
parentheses.pop()

append_parenthesis(0)
return parentheses_combinations