Describe
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If Such arrangement is not a possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must is in-place, do not allocate extra memory.
Example
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
Algorithm analysis
Difficulty : Medium
Analysis : Here we need to introduce a few related concepts:
Arrangement (arrangement), which is simply to remove M from N different elements, in a certain order in a column, usually denoted by a (m,n). When M=n, it is called the Full arrangement (permutation).
For example, for a collection a={1,2,3}, first get the fully arranged a1:1,2,3; then get the next permutation a2:1,3,2;
In this order, the full array of a is as follows, a total of 6 kinds:
a1:1,2,3; a2:1,3,2; a3:2,1,3; a4:2,3,1; a5:3,1,2; a6:3,2,1;
Mathematically, the total number A (n,n) = (N) * (N-1) *...*2*1=n!, but from a programmatic point of view, how do I get all the permutations? Then you have to get the next permutation in some order, usually in ascending order ( dictionary order lexicographically).
For any given arrangement, if you can find the next full permutation, it is easy to get all the permutations, that is, the next full permutation algorithm (next permutation)that the topic requires.
Ideas :
To set up a a:1,5,8,4,7,6,5,3,1 that currently has a set, the steps to take the next permutation are as follows:
/** Tips: next permuation based on the ascending order sort * sketch : * current: 1 5 8 4 7 6 5 3 1 * | | | * find i----+ j +----end * swap i and j : * 1 5 8 5 7 6 4 3 1 * | | | * j----+ i +----end * reverse j+1 to end : * 1 5 8 5 1 3 4 6 7 * | | * find j----+ +----end * */
The specific methods are:
A) find the first adjacent element pair (i,i+1) from behind and meet A[i] < a[i+1].
Easy to know, at this point from J to end must be descending. Can be proved by contradiction, please prove it by yourself.
b) Look for a minimum J in [I+1,end] to satisfy a[i]<a[j].
Since [J,end] is descending, there must be a J that satisfies the above conditions, and the first J that satisfies the a[i]<a[j] relationship can be found from behind, and J must be the j to be searched.
c) Swap I with J.
At this point, I becomes the smallest element larger than I, because the next full permutation must be arranged in ascending order with the current arrangement, so select the smallest element instead of I.
Easy to know, after the Exchange [J,end] still satisfies descending sort.
d) Reverse [j,end]
Because [J,end] is in descending order at this time, it is reversed. Finally get the next full sort.
Note: If you do not find a matching adjacent element pair in step a, which is i=begin at this point, then the current [Begin,end] is in descending order, that is, there is no next full arrangement, so it is reversed to ascending.
Code samples (C #)
public void NextPermutation(int[] nums){ int i = nums.Length - 2; //末尾向前查找,找到第一个i,使得A[i] < A[i+1] while (i >= 0 && nums[i + 1] <= nums[i]) { i--; } if (i >= 0) { //从i下标向后找第一个j,使得A[i]<A[j] int j = nums.Length - 1; while (j >= 0 && nums[j] <= nums[i]) { j--; } //交换i,j Swap(nums, i, j); } //逆置j之后的元素 Reverse(nums, i + 1, nums.Length);} //逆置排序private void Reverse(int[] nums, int start, int end){ int i = start, j = end - 1; while (i < j) { Swap(nums, i, j); i++; j--; }}//交换private void Swap(int[] nums, int i, int j){ int temp = nums[i]; nums[i] = nums[j];
Complexity of
- time complexity :O (n).
- space complexity :O (1).
Appendix
- Series Catalog Index
- Code implementation (C # Edition)
- Correlation algorithm: Permutation Sequence
Algorithmic title 丨 next permutation