-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoPlan0628
42 lines (40 loc) · 1.3 KB
/
AlgoPlan0628
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
#largest permutation
#key idea:
- for each ascending sub-array in the big array, find the right most first downward slope, and swap it with the largest value on its right.
- if there is no such int, return original.
- it is called lexigraphically larger
class Solution {
private int[] swap(int[] arr, int i ,int j ){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public int[] prevPermOpt1(int[] arr) {
int[] result = new int[arr.length];
int left = arr.length-1;
int right = 0;
int largest = 0;
for (; left >0; left--){
if (arr[left]< arr[left-1]){
//continue
//get the large one is left
//from its right
//get the second largest value
right = left;
largest = left-1;
while (right < arr.length && arr[right]< arr[left-1]){
if (arr[right] !=arr[right-1]) largest = right;
right++;
}
System.out.println(left-1);
System.out.println(largest);
result = swap(arr,left-1,largest);
return result;
}else{
//continue
}
}
return arr;
}
}