-
Notifications
You must be signed in to change notification settings - Fork 0
Generate Parentheses #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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): | ||
| if len(bracket_stack) == bracket_limit * 2: | ||
| return bracket_stack | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 関数名は |
||
|
|
||
| open_count = bracket_stack.count("(") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 毎回カウントしているため、時間計算量が n 倍増えてしまっていると思います。 open_count を引数で引き回してはいかがでしょうか?また、 close_count は open_count と blacket_limit から計算できると思います。 個人的には変数名は
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 新しい list を毎回作っているため、ここでも時間計算量が n 倍増えてしまっているように思います。 append して、 append_bracket() から返ってきたときに pop() してはいかがでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """ | ||
| nは定数だから引数に含めなくてよい | ||
| 関数名がparenthesisだから変数名もそれに合わせたほうが良い | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. まだbracketという単語使っていますよね。brackets_combinations, append_bracket
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bracket_limitは変化しないので、append_bracketのパラメータに含める必要はないです。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかに、定数として扱ってよいということですね。
ありがとうございます。
自分に欠けていた感覚でした。