-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/22. Generate Parentheses #52
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
base: main
Are you sure you want to change the base?
Conversation
| return list(parentheses) | ||
| ``` | ||
|
|
||
| - と思ったが(())(())のような部分的に囲むやつが考慮できていないので失敗 |
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.
こういう風にしたいならば、はじめの括弧と対応するやつは必ずあるはずなので、
(X)Y
と分ければいいですね。
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.
どういう実装をイメージされているんでしょうか?
再帰だとできないのかなと思ったんですが、
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.
ご参考リンクです。
olsen-blue/Arai60#54 (comment)
| ```python | ||
| class Solution: | ||
| def generateParenthesis(self, n: int) -> List[str]: | ||
| if n == 1: |
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.
n == 0 で [""] のほうがいいでしょう。
| - 時間計算量:O(n^n) | ||
| - 各層の計算量が<2k C k | ||
| - 2k C k < k^k なので大体このくらい? | ||
| - 空間計算量:O(n^n) |
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.
これはちょっと大きすぎる見積もりです。
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.
ですね
下でもう少し正しい見積もりをしてみています
| parentheses = set() | ||
| parenthesis_opened_closed = [("", 0, 0)] | ||
| while parenthesis_opened_closed: | ||
| parenthesis, opened, closed = parenthesis_opened_closed.pop() |
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.
opened で開き括弧の数を表しているのは少しわかりにくいと思いました。自分で書くなら num_opens にします。
| if opened == n: | ||
| new_parenthesis = parenthesis + (opened - closed) * ")" | ||
| if new_parenthesis not in parentheses: | ||
| parentheses.add(new_parenthesis) |
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.
if 文不要で直接 add しても良いと思いました。
| parentheses_and_openings = [("", 0)] | ||
| for opened in range(1, n + 1): | ||
| new_parentheses_and_openings = [] | ||
| for parenthesis, opening in parentheses_and_openings: |
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.
opened はこれまでに使った開き括弧の数で、opening はまだ対応する閉じ括弧がない開き括弧の数ということですよね。少し読み取りにくく感じました。下で書かれているようにこれまでに使った開き・閉じ括弧の数を考えた方がシンプルだと思いました。
問題:https://leetcode.com/problems/generate-parentheses/description/