-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0022-generate-parentheses.md #54
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
| if close_remain > open_remain: | ||
| stack.append((path + [")"], open_remain, close_remain - 1)) | ||
|
|
||
| return results |
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.
読みやすいです。
|
|
||
| ### 実装5 | ||
|
|
||
| - Backtrackingをloopで実装。 |
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.
僕の認識が間違っていたら申し訳ないんですが、新しくpathを二つ生成して次に残しておくような感じになっていて、「後戻り」をしている感じではないので、厳密に言えばいわゆるバックトラックとは少し違うかもしれないと思いました。
| path.pop() | ||
|
|
||
| dfs(0, 0) | ||
| return results |
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.
下のコードとは逆に、これはdfsというよりはbacktrackにあたるのではないかと思います。
ロジックは読みやすいと思います。open_used, close_usedは個数ということを分かりやすくするためにnum_open, num_closeなどでもいいかもしれないと思いました。
|
|
||
| ### 実装1 | ||
|
|
||
| - 貪欲法(枝刈りつきBFS) |
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.
BFS を貪欲法と呼ぶのは違和感を感じました。貪欲法は、その場その場で良さそうな選択肢を取り、最終的に最適解を得る手法だと思います。
この問題:https://leetcode.com/problems/generate-parentheses/
次の問題:https://leetcode.com/problems/move-zeroes/