-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextPermutation.py
More file actions
43 lines (27 loc) · 869 Bytes
/
nextPermutation.py
File metadata and controls
43 lines (27 loc) · 869 Bytes
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
def nextPerm(A):
def binarySearch (A, start, end, value):
if end >= start:
mid = (end+start)//2
if value < A[mid] and value >= A[mid-1]:
return A[mid]
elif A[mid] > value:
return binarySearch(A, start, mid - 1, value)
else:
return binarySearch(A, mid + 1, end, value)
else:
return -1
for i in range(len(A)-1, -1, -1):
val = binarySearch(A[i::], 0, len(A)-i-1, A[i])
if val != -1:
new = A.pop(i)
A[i] = val
A.append(new)
A[i+1::].sort()
break
return A
inp = list(map(int, input().split()))
L = []
for _ in range(inp[1]):
L.append(list(map(int, input().split())))
for arr in L:
print(' '.join(map(str, nextPerm(arr))))