Skip to content

Create 0022-generate-parentheses.md - #54

Open
naoto-iwase wants to merge 1 commit into
mainfrom
0022-generate-parentheses
Open

Create 0022-generate-parentheses.md#54
naoto-iwase wants to merge 1 commit into
mainfrom
0022-generate-parentheses

Conversation

@naoto-iwase

Copy link
Copy Markdown
Owner

if close_remain > open_remain:
stack.append((path + [")"], open_remain, close_remain - 1))

return results

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読みやすいです。


### 実装5

- Backtrackingをloopで実装。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

僕の認識が間違っていたら申し訳ないんですが、新しくpathを二つ生成して次に残しておくような感じになっていて、「後戻り」をしている感じではないので、厳密に言えばいわゆるバックトラックとは少し違うかもしれないと思いました。

path.pop()

dfs(0, 0)
return results

@potrue potrue Nov 4, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

下のコードとは逆に、これはdfsというよりはbacktrackにあたるのではないかと思います。
ロジックは読みやすいと思います。open_used, close_usedは個数ということを分かりやすくするためにnum_open, num_closeなどでもいいかもしれないと思いました。


### 実装1

- 貪欲法(枝刈りつきBFS)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BFS を貪欲法と呼ぶのは違和感を感じました。貪欲法は、その場その場で良さそうな選択肢を取り、最終的に最適解を得る手法だと思います。

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.

3 participants