Skip to content

31. Next Permutation #56

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

31. Next Permutation #56

wants to merge 1 commit into from

Conversation

fhiyo
Copy link
Owner

@fhiyo fhiyo commented Aug 24, 2024

i -= 1
if i == -1:
# nums is sorted descending order
nums.reverse()

Choose a reason for hiding this comment

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

下の nums[i + 1:] = reversed(nums[i + 1:]) と揃っていないのが若干気になりました。ただ、nums = reversed(nums)だと違ってしまうので、こうなってしまうのもわかります。
nums[:] = reversed(nums[:]) とするかどうか。

ちなみに、このreverseを一つにまとめてしまって、先に入れ替えをやってから、i > -1の場合に限りi番目の要素を適切に入れ替える、という実装もできます。そうするとこの部分は少しきれいになります。

nums[left], nums[right] = nums[right], nums[left]
nums[left + 1:] = reversed(nums[left + 1:])

def _is_decending_order_from(self, seq: Sequence[int]) -> int:

Choose a reason for hiding this comment

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

is_という関数名だとboolが返ってくるかと思ってしました。
find_とかどうでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

参考にしたC++のstd::is_sorted_until も真偽値を返すわけではないし、どのくらい気にするものなのかなと思っているところではありますね...
isだけ見たら真偽値だけどseq is descending order from 〇〇 の空欄を埋めると思えばいいんじゃないかなと思ってるんですがどうでしょうか (そこの関数名typoしてましたね)。

findだとあるかないか分からないものを探すというイメージがあり、Noneを返し得る関数に使うイメージでした。

if left == -1:
nums.reverse()
return
right = bisect_left(nums, -nums[left], left + 1, key=lambda x: -x) - 1

Choose a reason for hiding this comment

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

2分探索でも探せるんですね。勉強になりました。

Copy link

Choose a reason for hiding this comment

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

数が少ないと意外と頭から探したほうが速いかもしれません。そして、原理的に数が少ない場合がほとんどでしょう。

```py
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
def is_reversed_sorted_until(seq: Sequence[int]) -> int:
Copy link

@Mike0121 Mike0121 Aug 26, 2024

Choose a reason for hiding this comment

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

関数名、自分が書いた時もかなり悩んだのですが、reversedよりもstep3にある通りdescendingとかの方が良いなと思いました。
また、質問になってしまいますが、型をListではなくSequenceにしている理由は何かあるんでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

引数は広い型で指定した方が使える場面が増えるかなと思って、一応そうしてみてます。
(inner functionだと微妙ですが、) たとえばsequenceを指定しておけばtupleとかも渡せて保守で嬉しいかなと。

参考までに...
https://docs.python.org/3/glossary.html#term-sequence

An iterable which supports efficient element access using integer indices via the getitem() special method and defines a len() method that returns the length of the sequence. Some built-in sequence types are list, str, tuple, and bytes.

collectionモジュールの抽象基底クラスの一覧: https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes

Choose a reason for hiding this comment

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

参考までありがとうございます!レビュー時に気になって調べて初めて知りました。
下記もなるほどです。

引数は広い型で指定した方が使える場面が増えるかなと思って、一応そうしてみてます。

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.

5 participants