We find the first pair from the right where nums[i] < nums[i+1]. This i is the peak to be changed. Then find the smallest number in the right suffix that is larger than nums[i], swap them, and reverse the suffix.
- Find
i = len(nums) - 2such thatnums[i] < nums[i+1]. - If no such
i(falling sequence), reverse the whole array. - If
iexists:- Find
j = len(nums) - 1such thatnums[j] > nums[i]. - Swap
nums[i]andnums[j]. - Reverse the suffix
nums[i+1:].
- Find
- Time Complexity: O(N).
- Space Complexity: O(1).
def next_permutation(nums):
n = len(nums)
i = n - 2
while i >= 0 and nums[i] >= nums[i + 1]:
i -= 1
if i >= 0:
j = n - 1
while nums[j] <= nums[i]:
j -= 1
nums[i], nums[j] = nums[j], nums[i]
# Reverse suffix
left, right = i + 1, n - 1
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1