image_2021-11-02_17-52-09.png
64.8 KB
#medium
#N969. Pancake Sorting
problem link=>https://leetcode.com/problems/pancake-sorting/
#solution
#N969. Pancake Sorting
problem link=>https://leetcode.com/problems/pancake-sorting/
#solution
class Solution {
public List<Integer> pancakeSort(int[] arr) {
ArrayList<Integer> ans = new ArrayList<Integer>();
int indexMax=0, max=arr.length, temp=0;
for(int i=0; i<arr.length-1; i++){
for(int j=0; j<arr.length-i; j++){
if(arr[j]==max) indexMax=j+1;
}
if(arr[0]!=max){
for(int k = 0; k < indexMax / 2; k++){
temp = arr[k];
arr[k] = arr[indexMax - k - 1];
arr[indexMax - k - 1] = temp;
}
ans.add(indexMax);
}
ans.add(max);
for(int k = 0; k < max / 2; k++){
temp = arr[k];
arr[k] = arr[max - k - 1];
arr[max - k - 1] = temp;
}
max--;
}
return ans;
}
}