LeetCode – Next Permutation (Python)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
|
1 2 3 4 |
1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 |
The solution is the Pandit’s algorithm
1 Pandit’s algorithm
The article in Wikipedia describes the algorithm invented by Narayana Pandit to changes the list in-place to generate the next permutation given a list or Array A.
- Find the largest index
isuch that A[i] < A[i + 1]. If no such index exists, the permutation is the last permutation. In this case, we just reverse the list to get the next permutation.
12345678910#the largest index where A[i] < A[i + 1]# -1 if there is no such i where A[i] < A[i + 1]def get_split_index(A):i = len(A) - 1while i > 0:if A[i - 1] >= A[i]:i -= 1else:breakreturn i - 1 - Find the largest index
jgreater thanisuch that A[j] > A[i].
12345678def get_chang_index(A, i):j = len(A) - 1while j >= i:if A[j] > A[i]:breakelse:j -= 1return j - Swap the value of A
[i]with that of A[j].
123def swap(A, i, j):A[i], A[j] = (A[j], A[i]) - Reverse the remaining sequence from A
[i + 1]up to the end of the list.
12345678#reverse A[start:]def reverse(A, start):left = startright = len(A) - 1while left < right:swap(A, left, right)left += 1right -= 1
2 Example
Let’s put them together, and use A = [1, 2, 3, 4] to demonstrate the sequence of calling the get next permutation method.
|
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 |
#the largest index where A[i] < A[i + 1] # -1 if there is no such i where A[i] < A[i + 1] def get_split_index(A): i = len(A) - 1 while i > 0: if A[i] < A[i - 1]: i -= 1 else: break return i - 1 def get_change_index(A, i): j = len(A) - 1 while j >= i: if A[j] > A[i]: break else: j -= 1 return j def swap(A, i, j): A[i], A[j] = (A[j], A[i]) #reverse A[start:] def reverse(A, start): left = start right = len(A) - 1 while left < right: swap(A, left, right) left += 1 right -= 1 def next_permutation(A): split_index = get_split_index(A) # the Array is sorted in descreased order if split_index == -1: reverse(A, 0) else: change_index = get_change_index(A, split_index) swap(A, split_index, change_index) reverse(A, split_index + 1) A = [1,2,3,4] for i in range(30): next_permutation(A) print A |
The output is :
|
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 |
[1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1] [1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] |











