Skip to content

Create 31. Next Permutation.md#15

Merged
Mike0121 merged 1 commit intomainfrom
31.-Next-Permutation
Nov 1, 2025
Merged

Create 31. Next Permutation.md#15
Mike0121 merged 1 commit intomainfrom
31.-Next-Permutation

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

Comment on lines 76 to 80
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. 後ろ側から順に見て降順になっている最初のindexを返す
  2. 後ろ側から順に見て、見つけたindexにある要素より大きい最初の要素のindexを返す
  3. 1と2で見つけた要素をswapする
  4. 1の位置より後ろを逆順にする

なので、この部分を関数化してもいいのかもしれないです。
reverse_after(nums, index) とかですかね...

↑に関連するのでここに書くと、find_larger_index_of_subarr(nums, left) も微妙にやってることと名前が合ってないように見えました。実際はleftより後ろの要素のうち、nums[left]より大きい一番後ろの要素のindexを返しているので、 rfind_larger_index_of_subarr とか find_last_larger_index_of_subarr とかになるのかな?と思いました (名前はあんまり自信ないです...)

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.

fhiyoさん、丁寧にありがとうございます。"4. 1の位置より後ろを逆順にする"の部分が、たしかに冗長ですね。shining-aiさんにもご指摘いただいた通り、リバースしているだけなのでnums[left+1:] = nums[left+1:][::-1]で書き換えてしまうか、関数化した方が良いですね。

また、命名に関してですが、ご指摘の通りこの問題の関数はかなり難しく感じました。ご提案ありがとうございます。ご教示いただいた名前の方が通りやすいと思います。

Comment on lines 64 to 65
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

leftがfind_pos_desc_in_reversed()で見つけた値であることを知っていないと、leftより左の位置を返したり最悪 (arr[left]がリストの最大値なら) マイナスに突入してIndexErrorになる可能性もあるなと思いました。

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.

fhiyoさん、あリがとうございます。
自分でも書いていて、find_pos_desc_in_reversedから取得した値を入れないとErrorを起こす場合があるなと思いました。このような場合、どのようにするのがベストプラクティスなんでしょうかね。。。

下記のように関数を書き換えて、入力値が範囲外、もしくは適切な値でない場合-1を返すようにして、エラーを起こさせるなどを考えましたが、最適かはあまり自信がないです。

def find_larger_index_of_subarr(arr: List[int], left: int) -> int:
    if left < 0 or left >= len(arr) - 1:  # leftが範囲外である場合、-1を返す
        return -1

    i = len(arr) - 1
    while i > left and arr[i] <= arr[left]:
        i -= 1
    return i if i > left else -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.

どうするのがいいんでしょうね...ただ最低限、関数の使い方のコメントが書いてあればコードを見た人は取扱い方が分かるのでいいんじゃないかと思いました。

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

Choose a reason for hiding this comment

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

reverseで良いような気がするのですが、何か理由ありますか?

Copy link
Copy Markdown
Owner Author

@Mike0121 Mike0121 May 19, 2024

Choose a reason for hiding this comment

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

いつもありがとうございます。たしかにここ、
nums[left+1:] = nums[left+1:][::-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.

(スライスでコピーが発生するのを避けたのかなと思ってました)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

試しにどちらが速いか調べてみたらどうでしょう。私はほとんど変わらないけれどもスライスな気がします。

@Mike0121 Mike0121 changed the title 31. Next Permutation.md Create 31. Next Permutation.md Nov 1, 2025
@Mike0121 Mike0121 merged commit fd22524 into main Nov 1, 2025
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