Conversation
| break | ||
|
|
||
| if pivot == -1: | ||
| nums[:] = list(reversed(nums)) |
There was a problem hiding this comment.
こちら、追加のリストオブジェクトを新規作成するので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.
ここは、list.reverse()をそのまま適用できないので、Two Pointersを使ってswapしていくin-placeなreverseの自前実装が必要ですね。
もし思いつかない場合は olsen-blue/Arai60#59 などの実装が参考になると思います。
There was a problem hiding this comment.
ご指摘ありがとうございます。
空間計算量の条件を見落としていたので、その条件に満たすように修正してみました。
d30d5e7
There was a problem hiding this comment.
step 4拝見しました。
for-ifの中に書くとネストが深くて読みにくいと感じました。また、iを使うわけでもないので不自然に感じました。
単に元のnums[pivot + 1:] = list(reversed(nums[pivot + 1:]))を置き換えるのが良いと思いました。
There was a problem hiding this comment.
以下が参考になると思います。
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