2
def nextPermutation(self, nums) -> None:3
firstDecreasingElement = -15
lastIndex = len(nums) - 17
# Looking for an element that is less than its follower8
for i in range(lastIndex, 0, -1):9
if nums[i] > nums[i - 1]:10
firstDecreasingElement = i - 113
# If there is not any then reverse the array to make initial permutation14
if firstDecreasingElement == -1:15
for i in range(0, lastIndex // 2 + 1):16
nums[i], nums[lastIndex - i] = nums[lastIndex - i], nums[i]19
# Looking for an element to swap it with firstDecreasingElement20
for i in range(lastIndex, 0, -1):21
if nums[i] > nums[firstDecreasingElement]:26
nums[firstDecreasingElement], nums[toSwapWith] = (28
nums[firstDecreasingElement],31
# Reverse elements from firstDecreasingElement to the end of the array32
left = firstDecreasingElement + 135
nums[left], nums[right] = nums[right], nums[left]