Skip to content

Conversation

@skypenguins
Copy link
Owner

31. Next Permutation

次回予告: 33. Search in Rotated Sorted Array

@skypenguins skypenguins self-assigned this Nov 24, 2025
break

if pivot == -1:
nums[:] = list(reversed(nums))

Choose a reason for hiding this comment

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

こちら、追加のリストオブジェクトを新規作成するのでconstant extra memoryになっていないと思います。
nums.reverse()ならin-placeかつconstant extra memoryでやってくれます。

list.reverse()
Reverse the elements of the list in place.

https://docs.python.org/3.14/tutorial/datastructures.html#more-on-lists

下のnums[pivot + 1:] = list(reversed(nums[pivot + 1:]))も同じですね。

nums[i], nums[pivot] = nums[pivot], nums[i]
break

nums[pivot + 1:] = list(reversed(nums[pivot + 1:]))
Copy link

@docto-rin docto-rin Nov 24, 2025

Choose a reason for hiding this comment

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

ここは、list.reverse()をそのまま適用できないので、Two Pointersを使ってswapしていくin-placeなreverseの自前実装が必要ですね。

もし思いつかない場合は olsen-blue/Arai60#59 などの実装が参考になると思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます。
空間計算量の条件を見落としていたので、その条件に満たすように修正してみました。
d30d5e7

Choose a reason for hiding this comment

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

step 4拝見しました。

for-ifの中に書くとネストが深くて読みにくいと感じました。また、iを使うわけでもないので不自然に感じました。

単に元のnums[pivot + 1:] = list(reversed(nums[pivot + 1:]))を置き換えるのが良いと思いました。

Choose a reason for hiding this comment

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

以下が参考になると思います。

コードの整え方:https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.9kpbwslvv3yv

while A:
    短いX
    if B:
        長いY
        return C
短いZ
return D

なんですよね。

一般に、A と ZD の関係が強く、Y での処理が複雑だと、読みにくいでしょう。これは自然言語でもそうです。
...

https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860

@tokuhirat
Copy link

すでに指摘がある点以外は問題ないと思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants