forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNext Permutation.py
34 lines (29 loc) · 1.25 KB
/
Next Permutation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def nextPermutation(self, nums) -> None:
firstDecreasingElement = -1
toSwapWith = -1
lastIndex = len(nums) - 1
# Looking for an element that is less than its follower
for i in range(lastIndex, 0, -1):
if nums[i] > nums[i - 1]:
firstDecreasingElement = i - 1
break
# If there is not any then reverse the array to make initial permutation
if firstDecreasingElement == -1:
for i in range(0, lastIndex // 2 + 1):
nums[i], nums[lastIndex - i] = nums[lastIndex - i], nums[i]
return
# Looking for an element to swap it with firstDecreasingElement
for i in range(lastIndex, 0, -1):
if nums[i] > nums[firstDecreasingElement]:
toSwapWith = i
break
# Swap found elements
nums[firstDecreasingElement], nums[toSwapWith] = nums[toSwapWith], nums[firstDecreasingElement]
# Reverse elements from firstDecreasingElement to the end of the array
left = firstDecreasingElement + 1
right = lastIndex
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1