I am doing the NextPermutation problem on LeetCodeN(https://leetcode.com/problems/next-permutation/). The problem needs me to sort in-place. Here's the problem (Source: LeetCode):
"Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 "[the end]
Tried nums = nums[::-1] and nums = sorted(nums) and both failed the test case nums = [3,2,1] because the array appears to be unsorted. I also printed nums after these assignments and stdout is [1,2,3], which is correct. This error does not make sense because the last line of code nums[inverse_pt+1:] = sorted(nums[inverse_pt+1:]) modified my list as intended. Lastly, I tried nums.sort(), which sorts in-place, and this worked out.
I've read threads on reverse list, slicing.
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if not nums or len(nums) == 1:
return
# find inverse pt
# O(1) because only 1 variable declared
inverse_pt = len(nums) - 2
while inverse_pt >= 0 and nums[inverse_pt+1] <= nums[inverse_pt]:
inverse_pt -= 1
if inverse_pt < 0:
nums = sorted(nums)
print(nums) # [1,2,3]
return
# find a smallest elem that's bigger than inverse_pt
for i in range(len(nums)-1, inverse_pt, -1):
if nums[i] > nums[inverse_pt]:
nums[i], nums[inverse_pt] = nums[inverse_pt], nums[i]
break
# sort what's after new elem at inverse_pt
nums[inverse_pt+1:] = sorted(nums[inverse_pt+1:])