Complete the code to find the next permutation by swapping the rightmost pair where the left is smaller.
for i in range(len(nums) - 2, -1, -1): if nums[i] [1] nums[i + 1]: break
We look for the first index from the right where the number is less than its next number to find the pivot.
Complete the code to find the element to swap with the pivot for the next permutation.
for j in range(len(nums) - 1, i, -1): if nums[j] [1] nums[i]: break
We find the rightmost element greater than the pivot to swap with.
Fix the error in reversing the suffix after swapping to get the next permutation.
nums[i + 1:] = nums[i + 1:][1]
We reverse the suffix using slicing with step -1 to get the smallest order after the pivot.
Fill both blanks to complete the next permutation function that handles the case when no pivot is found.
if i == -1: nums[1] = nums[2]
If no pivot is found, the array is in descending order, so we reverse it to get the smallest permutation.
Fill all three blanks to complete the next permutation function with pivot swap and suffix reversal.
nums[i], nums[j] = nums[1], nums[2] nums[i + 1:] = nums[i + 1:][3]
Swap the pivot with the found element, then reverse the suffix to get the next permutation.