File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > pancakeSort (int [] A ) {
3
+ List <Integer > ans = new ArrayList <>();
4
+ int [] copy = Arrays .copyOf (A , A .length );
5
+ int n = A .length - 1 ;
6
+ Arrays .sort (A );
7
+ int greatest = A .length - 1 ;
8
+ for (int i = 0 ; i < A .length ; i ++) {
9
+ int idx = findIdx (copy , A [greatest ]);
10
+ flip (copy , idx );
11
+ ans .add (idx + 1 );
12
+ flip (copy , greatest );
13
+ ans .add (greatest + 1 );
14
+ greatest --;
15
+ }
16
+
17
+ return ans ;
18
+ }
19
+
20
+ private void flip (int [] copy , int idx ) {
21
+ int start = 0 ;
22
+ int end = idx ;
23
+ while (start < end ) {
24
+ int temp = copy [start ];
25
+ copy [start ] = copy [end ];
26
+ copy [end ] = temp ;
27
+ start ++;
28
+ end --;
29
+ }
30
+ }
31
+
32
+ private int findIdx (int [] arr , int target ) {
33
+ for (int i = 0 ; i < arr .length ; i ++) {
34
+ if (arr [i ] == target ) {
35
+ return i ;
36
+ }
37
+ }
38
+
39
+ return -1 ;
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments