Skip to content

Conversation

@Ryotaro25
Copy link
Owner

@Ryotaro25 Ryotaro25 commented Mar 23, 2025

問題へのリンク
https://leetcode.com/problems/next-permutation/description/
問題文(プレミアムの場合)

備考

次に解く問題の予告
String to Integer (atoi)

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、lower_bound.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

Comment on lines +4 to +10
int first_increasing_order_index = nums.size() - 1;
while (first_increasing_order_index > 0) {
if (nums[first_increasing_order_index - 1] < nums[first_increasing_order_index]) {
break;
}
first_increasing_order_index--;
}
Copy link

Choose a reason for hiding this comment

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

これを見て、first_increasing_order_index の初期化がここで終了したという理解は構造からは得られないと思うんですよね。たとえば、下はどうでしょうか。

int first_increasing_order_index = 0;
for (int i = num.size() - 1; i > 0; --i) {
    if (...) {
       first_increasing_order_index = i;
        break;
    }
}

これは、0 がデフォルトで後ろから条件を満たすものを探して初期化したと読めますね。

const int first_increasing_order_index = get_first_increasing_order(nums);

これも初期化したと読めますね。

初期化するために繰り返し変更すると意図が追いにくくなるでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda

初期化するために繰り返し変更すると意図が追いにくくなるでしょう。
改めて読むと何のための処理なのかコードから追えない、レビュワー(ユーザー)にとって優しくないコードでした。

きれいなコードを GitHub に上げることが目的になっている感触を微かに持っています。
この部分も含めコードを書き直して、step4に上げてみました🙇
7c97dc1

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.

@oda なるほどです。メタ認知力を鍛えたいと思ってそう言ったタイトルの本を読んでみたのですが、おすすめの本などあったりしますでしょうか?

Copy link

Choose a reason for hiding this comment

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

よく分かりません。
いや、まあ、でも一般的に国語力といわれるものが大事ですかねえ。

next_permutation関数を使うのは、求めらていないだろう

## ステップ3
**3回書き直しやりましょう、といっているのは、不自然なところや負荷の高いところは覚えられないからです。**
Copy link

Choose a reason for hiding this comment

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

なんとなく解くことが目的化しているというか、きれいなコードを GitHub に上げることが目的になっている感触を微かに持っています。(オンラインなのではっきりとは分かりません。)
この部分は、私がここで練習して欲しいことの2,3割です。
私が訓練したいのは、反応をするようになるとか感情を持てるようになるなどの内心です。
参加マニュアルを一回読み直してみてください。
http://docs.google.com/document/d/1bjbOSs-Ac0G_cjVzJ2Qd8URoU_0BNirZ8utS3CUAeLE

ただ、好きなようにやればいいとは思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
一度マニュアルを読み直します。

pivot以降を逆順に並び変える箇所について
元々pivot以降は降順に並んでいるので、reverseを使い結果として昇順に並び替えているが
sortを使った方がわかりやすいような気がする
o(n)からO(n log n)になるが、昇順に並び替えているというのは伝わりそう
Copy link

Choose a reason for hiding this comment

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

小文字の o は意味が変わるので注意です。


private:
// 右側が降順になっている範囲の直前のインデックスを探す
int FindDecreasingIndex(const vector<int>& nums) {
Copy link

Choose a reason for hiding this comment

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

nextPermutationとこちらの関数名のネーミングルールが揃っていないことに違和感があります。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@usatie
レビューありがとうございます。
この会では可能な限りGoogleのスタイルガイドに合わせてコードを書いております。これはこの会以外ではC++を使うことがないためです。

全体で統一できればなのですが、LeetCodeで用意されている部分は仕方ないかなと思っております。
https://google.github.io/styleguide/cppguide.html#Function_Names

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