-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays4.py
48 lines (36 loc) · 1.41 KB
/
arrays4.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Next Permutation
# Given an array of integers arr[] representing a permutation, implement the next permutation that rearranges the numbers into the lexicographically next greater permutation. If no such permutation exists, rearrange the numbers into the lowest possible order (i.e., sorted in ascending order).
# Note - A permutation of an array of integers refers to a specific arrangement of its elements in a sequence or linear order.
# Examples:
# Input: arr = [2, 4, 1, 7, 5, 0]
# Output: [2, 4, 5, 0, 1, 7]
# Input: arr = [3, 2, 1]
# Output: [1, 2, 3]
# Input: arr = [3, 4, 2, 5, 1]
# Output: [3, 4, 5, 1, 2]
# Time Complexity: O(n) and Space Complexity: O(1)
class Solution:
def next_permutation(self, arr):
n = len(arr)
pivot = -1
for i in range(n - 2, -1, -1):
if arr[i] < arr[i + 1]:
pivot = i
break
if pivot == -1:
arr.reverse()
return arr
for i in range(n - 1, pivot, -1):
if arr[i] > arr[pivot]:
arr[i], arr[pivot] = arr[pivot], arr[i]
break
left, right = pivot + 1, n - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
solution = Solution()
arr = [2, 4, 1, 7, 5, 0]
solution.next_permutation(arr)
print(" ".join(map(str, arr)))