Skip to content

Create 22. Generate Parentheses.md#52

Open
fuga-98 wants to merge 1 commit into
mainfrom
22.-Generate-Parentheses
Open

Create 22. Generate Parentheses.md#52
fuga-98 wants to merge 1 commit into
mainfrom
22.-Generate-Parentheses

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 26, 2025

if num_open >= n and num_close >= n:
result.append(parentheses)
if num_open < n:
stack.append((num_open + 1, num_close, f'{parentheses}('))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

文字列を連結する際、 + 演算子 1 つで済むのであれば、 + で結合しても、十分シンプルかつ読みやすいと思います。複数個になった場合は、読みやすさに応じて f 文字列に切り替えたほうがよいと思います。

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.

レビューありがとうございます。
結合する文字列がかっこなのもあいまって、+演算子のほうが読みやすく感じました。

- コピーの際の計算量。
- Step1のソースはコピーが発生してますね。
- 今回の問題では気にしなくても良さそうか?
- カタラン数は常識ではないらしい。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

カタラン数は聞いたことありました?
大学受験でたまに出てくることがありますね。(知らなくても解ける程度。)

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.

知らなかったです。
思ったより応用範囲が広そうに思いました。

https://www.chugakujuken.com/koushi_blog/sakai/21731.html
https://ja.m.wikipedia.org/wiki/%E3%82%AB%E3%82%BF%E3%83%A9%E3%83%B3%E6%95%B0

return result
```

紙に書きながらデバッグしたらなんとなくわかった。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

それほど問題にならない気もしますが、文字列の生成が何度も行われていますね。

Copy link
Copy Markdown
Owner Author

@fuga-98 fuga-98 May 26, 2025

Choose a reason for hiding this comment

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

言われてみればそうでした。
計算量の観点では文字列ではなくリストで作っていって最後にjoinしたほうが良さそうです。


```python
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@cache デコレータをつけると速くなりますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

文字列を丸ごとコピーしないようにするやつを、Python にしてみました。
https://discord.com/channels/1084280443945353267/1262688866326941718/1363826801016701018

from functools import cache
from typing import List, Callable

@cache
def generate_functions_to_append_parenthesis(n: int) -> List[Callable[[List[str]], None]]:
    if n == 0:
        return [lambda sb: None]
    result = []
    for i in range(n):
        for a in generate_functions_to_append_parenthesis(i):
            for b in generate_functions_to_append_parenthesis(n - i - 1):
                def fn(sb: List[str], a=a, b=b) -> None:
                    sb.append("(")
                    a(sb)
                    sb.append(")")
                    b(sb)
                result.append(fn)
    return result

def generate_parenthesis(n: int) -> List[str]:
    funcs = generate_functions_to_append_parenthesis(n)
    result = []
    for f in funcs:
        sb = []
        f(sb)
        result.append("".join(sb))
    return result

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