-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermutation.java
More file actions
74 lines (69 loc) · 1.62 KB
/
Copy pathNextPermutation.java
File metadata and controls
74 lines (69 loc) · 1.62 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers for a given array A of size N.
*
* If such arrangement is not possible, it must be rearranged as the lowest possible order, i.e., sorted in ascending order.
*
* NOTE:
*
* The replacement must be in-place, do not allocate extra memory.
* DO NOT USE LIBRARY FUNCTION FOR NEXT PERMUTATION. Use of Library functions will disqualify your submission retroactively and will give you penalty points.
*
*
* Problem Constraints
* 1 <= N <= 5 * 105
*
* 1 <= A[i] <= 109
*
*
*
* Input Format
* The first and the only argument of input has an array of integers, A.
*
*
*
* Output Format
* Return an array of integers, representing the next permutation of the given array.
*
*
*
* Example Input
* Input 1:
*
* A = [1, 2, 3]
* Input 2:
*
* A = [3, 2, 1]
*
*
* Example Output
* Output 1:
*
* [1, 3, 2]
* Output 2:
*
* [1, 2, 3]
*/
public class Solution {
public int[] nextPermutation(int[] A) {
int n = A.length;
for(int i = n - 2; i >= 0; i--){
if(A[i] < A[i + 1]) {
swapWithNextElem(A, i);
Arrays.sort(A, i + 1, n);
return A;
}
}
Arrays.sort(A);
return A;
}
private void swapWithNextElem(int[] A, int elemIdx){
int idx = elemIdx + 1;
for(int i = elemIdx + 1; i < A.length; i++){
if(A[elemIdx] < A[i] && A[idx] > A[i])
idx = i;
}
int temp = A[idx];
A[idx] = A[elemIdx];
A[elemIdx] = temp;
}
}