Skip to content

Conversation

@Satorien
Copy link
Owner

return list(parentheses)
```

- と思ったが(())(())のような部分的に囲むやつが考慮できていないので失敗
Copy link

Choose a reason for hiding this comment

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

こういう風にしたいならば、はじめの括弧と対応するやつは必ずあるはずなので、
(X)Y
と分ければいいですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

どういう実装をイメージされているんでしょうか?
再帰だとできないのかなと思ったんですが、

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:
Copy link

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)
Copy link

Choose a reason for hiding this comment

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

これはちょっと大きすぎる見積もりです。

Copy link
Owner Author

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()

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)

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:

Choose a reason for hiding this comment

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

opened はこれまでに使った開き括弧の数で、opening はまだ対応する閉じ括弧がない開き括弧の数ということですよね。少し読み取りにくく感じました。下で書かれているようにこれまでに使った開き・閉じ括弧の数を考えた方がシンプルだと思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants