Conversation
| if len(bracket_stack) == bracket_limit * 2: | ||
| return bracket_stack | ||
|
|
||
| open_count = bracket_stack.count("(") |
There was a problem hiding this comment.
毎回カウントしているため、時間計算量が n 倍増えてしまっていると思います。 open_count を引数で引き回してはいかがでしょうか?また、 close_count は open_count と blacket_limit から計算できると思います。
個人的には変数名は num_opens にすると思います。 open_count でも良いと思います。
There was a problem hiding this comment.
ご指摘ありがとうございます。
本当ですね...修正します🙏
| open_count = bracket_stack.count("(") | ||
| close_count = bracket_stack.count(")") | ||
| if open_count < bracket_limit: | ||
| new_bracket_stack = list(bracket_stack) |
There was a problem hiding this comment.
新しい list を毎回作っているため、ここでも時間計算量が n 倍増えてしまっているように思います。 append して、 append_bracket() から返ってきたときに pop() してはいかがでしょうか?
| class Solution: | ||
| def generateParenthesis(self, n: int) -> List[str]: | ||
| answer = [] | ||
| def append_bracket(bracket_limit, bracket_stack): |
There was a problem hiding this comment.
bracket_limitは変化しないので、append_bracketのパラメータに含める必要はないです。
There was a problem hiding this comment.
たしかに、定数として扱ってよいということですね。
ありがとうございます。
自分に欠けていた感覚でした。
| answer = [] | ||
| def append_bracket(bracket_limit, bracket_stack): | ||
| if len(bracket_stack) == bracket_limit * 2: | ||
| return bracket_stack |
There was a problem hiding this comment.
関数名はgenerateParenthesisになっているので、bracketではなく、parenthesisに統一した方がいいでしょう。
arai60/generate-parentheses/step4.py
Outdated
| class Solution: | ||
| def generateParenthesis(self, n: int) -> List[str]: | ||
| brackets_combinations = [] | ||
| def append_bracket(parentheses, open_count): |
There was a problem hiding this comment.
parenthesesも、brackets_combinationsと同じく、パラメータに含める必要はないです。inner functionを使うメリットの一つは外のスコープの変数にアクセスできることです。パラメータの数は一般に少ない方がいいです。
There was a problem hiding this comment.
ご指摘ありがとうございます。
open_countも同じように関数の外に書いても動作すると思いますが、そこを引数として渡す理由は、値渡しだからと考えてよいですか?
すなわち、parenthesesは参照渡しで他に影響を与えてしまうので、それなら引数として渡さないほうが良いという解釈でよろしいでしょうか?
There was a problem hiding this comment.
正直そこまで考えていなかったのですが、確かにopen_countも外でも大丈夫ですが、 毎回、open_count += 1、open_count -= 1するのは冗長な気がします。 一方、parenthesesの場合は、append_bracketの1番目の引数は常にparenthesesなので、これもrepetitiveに感じました。
There was a problem hiding this comment.
なるほど、ありがとうございます。
感覚理解できました🙏
| @@ -0,0 +1,27 @@ | |||
| """ | |||
| nは定数だから引数に含めなくてよい | |||
| 関数名がparenthesisだから変数名もそれに合わせたほうが良い | |||
There was a problem hiding this comment.
まだbracketという単語使っていますよね。brackets_combinations, append_bracket
arai60/generate-parentheses/step4.py
Outdated
| close_count = len(parentheses) - open_count | ||
| if open_count < n: | ||
| parentheses.append("(") | ||
| append_bracket(parentheses, open_count+1) |
arai60/generate-parentheses/step4.py
Outdated
| class Solution: | ||
| def generateParenthesis(self, n: int) -> List[str]: | ||
| brackets_combinations = [] | ||
| def append_bracket(parentheses, open_count): |
There was a problem hiding this comment.
正直そこまで考えていなかったのですが、確かにopen_countも外でも大丈夫ですが、 毎回、open_count += 1、open_count -= 1するのは冗長な気がします。 一方、parenthesesの場合は、append_bracketの1番目の引数は常にparenthesesなので、これもrepetitiveに感じました。
| parentheses_combinations.append("".join(parentheses)) | ||
| return | ||
|
|
||
| close_count = len(parentheses) - open_count |
問題:https://leetcode.com/problems/generate-parentheses/description/
参考:
https://leetcode.com/problems/generate-parentheses/solutions/2542620/python-java-w-explanation-faster-than-96-w-proof-easy-to-understand/?envType=list&envId=me1nua2e
https://github.com/SuperHotDogCat/coding-interview/pull/7/files
https://github.com/shining-ai/leetcode/pull/53/files