Skip to content

22. Generate Parentheses#6

Merged
ryoooooory merged 5 commits intomainfrom
task/22
Jul 20, 2024
Merged

22. Generate Parentheses#6
ryoooooory merged 5 commits intomainfrom
task/22

Conversation

@ryoooooory
Copy link
Copy Markdown
Owner

・問題URL
https://leetcode.com/problems/generate-parentheses/

・課題/感想
ACはできたのですが、途中でおなじことの繰り返しによるTLEがでたので前の状態を保持するようにして解消しました。
ただ、時間計算量の見積もりができなくてなやんでいます

@@ -0,0 +1,31 @@
class Solution {
Copy link
Copy Markdown

@nodchip nodchip Apr 20, 2024

Choose a reason for hiding this comment

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

想定していたアルゴリズムとまったく違うアルゴリズムで、違和感を感じました。

  • current の末尾に「(」または「)」を付け加える。
  • current の末尾から「(」または「)」を取り除く。

の操作だけで書けませんか?途中、括弧を閉じすぎたり、最後に開きすぎたりしないかのチェックは必要だと思います。

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.

ありがとうございます!提案いただいた方法で実装してみました!アプローチや思考などはREADMEに記載していています!

@ryoooooory
Copy link
Copy Markdown
Owner Author

あっ2つ目の解法の部分で今気づきましたが、途中でのチェックの関数と最終チェックの関数について最後以外の処理は一緒なので、引数でinProcess: boolanとかを持たせてもいいかもですね

}

private boolean isValidParenethesisInProcess(String string) {
int leftParenethesisCount = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

leftParenethesisCount という変数名ですと、「(」の数を表しているように感じます。変数の中に含まれている値が表すものを、より直接的に表現した変数名にできますか?

Copy link
Copy Markdown
Owner Author

@ryoooooory ryoooooory Apr 21, 2024

Choose a reason for hiding this comment

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

ここは、おっしゃるとおり「(」の数を表していた:(正確にいうと文字列の中の"("をカウントする変数)のですが、僕の認識違いですかね?

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.

なるほど!現在の状態も正確に表すように含めるということですね!ありがとうございます!

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.

ちなみにこれは少し脱線気味ですが、関数名と変数名の意味の含み度合いについて目安などありますでしょうか?
今回の例だと、isValidParenethesisInProcessに、途中経過という意味が入っているので完全に閉じた関係ではないことも少し意味的に含んでいるようにもとらえられるかもと思いました(可読性の観点ではもちろん、一眼でわかったほうがいいので状態も含めたほうがいいのはもちろんなのですが)
プロジェクトだと大体こういう場合はコメントをつけて補足することが多いので気になった次第です!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数に含まれる値や、関数の内容が、必要十分に分かる端的な英単語・英語句を付けるのが良いと思います。

return parenthesisStrings;
}

private boolean isValidParenethesisInProcess(String string) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

generateParenthesis() の中で文字列を生成する際、同時に leftParenethesisCount も持たせれば、 isValidParenethesisInProcess() と isValidParenethesis() が不要になると思います。

異なる処理を分割したほうが実装がシンプルになることはあります。ですが、今回のケースでは処理内容が十分に単純なため、同時に行ったほうが良いと思います。

}

private boolean isValidParenethesisInProcess(String string) {
int notClosedleftParenethesisCount = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: notClosedleftParenethesisCount -> notClosedLeftParenethesisCount


while (!parenthesisCandidate.isEmpty()) {
String current = parenthesisCandidate.poll();
int currentLeftParenthesisCount = unClosedleftParentheses.poll();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

currentLeftParenthesisCount と、接尾辞に Count を付けても良いのですが、接頭辞に numberOf を付けるやり方盛ります。通例、 num と略して書くと思います。

numCurrentLeftParenthesis

この辺りは、チーム内で主流なやり方を調べ、それに合わせるのが良いと思います。また、複数の書き方ができるようにしておき、どれでも対応できるようにしておくとよいと思います。

class Solution {
public List<String> generateParenthesis(int n) {
List<String> allParentheses = new ArrayList<>();
Queue<String> parenthesisCandidate = new LinkedList<>();
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.

ありがとうございます!こちらはStringとIntegerを変数にもつクラスを定義して、そのオブジェクトをQueueに入れていくというイメージであっていますでしょうか?

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

@nodchip nodchip left a comment

Choose a reason for hiding this comment

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

よいと思います。


class ParenthesisState {
String str;
int numUnClosedLeftParenthesis;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

unclosed で 1 単語だと思います。 numUnclosedLeftParenthesis

public List<String> generateParenthesis(int n) {
List<String> allParentheses = new ArrayList<>();
Queue<Pair<String, Integer>> parenthesisCandidate = new LinkedList<>();
parenthesisCandidate.add(new Pair<>("", 0));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pair は、コードを読むにあたり、中に含まれている値を把握するのに認知負荷がかかるため、あまり使わないほうが良いと思います。代わりにユーザー定義クラスを使ったほうが良いと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(これは、私が試しにお願いしてみた解法です。)

allParentheses.add(currentState.getKey());
continue;
}
for (int i = 0; i <= numCurrentUnClosedLeftParenthesis + 1; i++) {
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 文字ずつ追加すれば十分だと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(これは、私が試しにお願いしてみた解法です。)

@ryoooooory ryoooooory merged commit 708f02a into main Jul 20, 2024
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