-
Notifications
You must be signed in to change notification settings - Fork 0
/
PancakeSort.cpp
45 lines (32 loc) · 1.05 KB
/
PancakeSort.cpp
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
//pancake sort in O(N)
//Reference https://leetcode.com/problems/pancake-sorting/discuss/494417/Dew-It-or-True-O(n)-or-Explained-with-Diagrams
class Solution {
public:
void add(vector<int>& ans,int i){
if(i>1){
if(!ans.empty() and ans.back()==i) ans.pop_back();
else ans.push_back(i);
}
}
vector<int> pancakeSort(vector<int>& a) {
int n=a.size();
vector<int> ans,id(n);
for(int i=0;i<n;i++) id[a[i]-1]=i;
for(int i=0;i<n;i++){
int p=id[i];
//do swaps
if(p!=i){
add(ans,p+1);
add(ans,p-i+1);
add(ans,p-i);
add(ans,p-i-1);
add(ans,p-i);
add(ans,p+1);
}
swap(a[i],a[p]);
id[a[i]-1]=i;
id[a[p]-1]=p;
}
return ans;
}
};