-
Notifications
You must be signed in to change notification settings - Fork 0
/
205.py
49 lines (38 loc) · 1.17 KB
/
205.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
49
"""
Problem:
Given an integer, find the next permutation of it in absolute order. For example, given
48975, the next permutation would be 49578.
"""
from typing import List
def get_next_helper(arr: List[int]) -> List[int]:
length = len(arr)
if length < 2:
return arr
# finding the last element arranged in ascending order
for index in range(length - 1, -1, -1):
if index > 0 and arr[index - 1] < arr[index]:
break
# if index is 0, arr is sorted in descending order
if index == 0:
arr.reverse()
return arr
# finding the next permutation
for k in range(length - 1, index - 1, -1):
if arr[k] > arr[index - 1]:
arr[k], arr[index - 1] = arr[index - 1], arr[k]
break
# arranging the other elements in proper order
size = (length - 1) + index
for i in range(index, (size + 1) // 2):
arr[i], arr[size - i] = arr[size - i], arr[i]
return arr
def get_next(num: int) -> int:
return int("".join(get_next_helper(list(str(num)))))
if __name__ == "__main__":
print(get_next(48975))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
[n = number of digits]
"""