Next Permutation [LC#31]
Given an array of integers nums, find the next permutation of nums.
The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
Intuition
- [Step 1] Find the largest
isuch thatnums[i] < nums[i+1]. If no such index exists, then this is the last sequence in the permutation. Reset array to sorted order (first element in the sequence). - [Step 2] Find the largest index
j > isuch thatnums[i] < nums[j]. Swap these 2 numbers - [Step 3] Reverse the array from
i+1till the end.
Code
def next_permutation(self, nums: List[int]) -> List[int]:
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]
def reverse(start, end):
nums[start : end + 1] = nums[start : end + 1 : -1]
n = len(nums)
# Step 1
i = n - 2
while i >= 0 and nums[i] >= nums[i + 1]:
i -= 1
if i >= 0:
# Step 2
j = n - 1
while nums[i] >= nums[j]:
j -= 1
swap(i, j)
# Step 3
reverse(i + 1, n - 1)
return nums
Time complexity
- $T(n) = O(n)$
- $S(n) = O(1)$