-
Notifications
You must be signed in to change notification settings - Fork 0
31. Next Permutation #28
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
base: main
Are you sure you want to change the base?
Conversation
| break | ||
|
|
||
| if pivot == -1: | ||
| nums[:] = list(reversed(nums)) |
There was a problem hiding this comment.
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:])) |
There was a problem hiding this comment.
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 などの実装が参考になると思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘ありがとうございます。
空間計算量の条件を見落としていたので、その条件に満たすように修正してみました。
d30d5e7
There was a problem hiding this comment.
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:]))を置き換えるのが良いと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下が参考になると思います。
while A: 短いX if B: 長いY return C 短いZ return Dなんですよね。
一般に、A と ZD の関係が強く、Y での処理が複雑だと、読みにくいでしょう。これは自然言語でもそうです。
...
https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860
|
すでに指摘がある点以外は問題ないと思いました。 |
31. Next Permutation
次回予告: 33. Search in Rotated Sorted Array