-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathpancake-sort.cpp
76 lines (67 loc) · 2.69 KB
/
pancake-sort.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
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
75
76
//Approach 1: Sort Largest to Smallest
//Runtime: 8 ms, faster than 69.75% of C++ online submissions for Pancake Sorting.
//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Pancake Sorting.
class Solution {
public:
vector<int> pancakeSort(vector<int>& A) {
vector<int> ans;
//skip curmax = 1 because 1 must be in correct place
// when all other elements all in correct place
for(int curmax = A.size(); curmax >= 2; curmax--){
int index = find(A.begin(), A.end(), curmax) - A.begin();
//make index 1-based
index += 1;
// cout << curmax << ", " << index << endl;
if(index > 1){
//"last" is exclusive, so A[0], ..., A[index-1]
reverse(A.begin(), A.begin() + index);
// copy(A.begin(), A.end(), ostream_iterator<int>(cout, " "));
// cout << endl;
ans.push_back(index);
}
if(curmax > 1){
//A[0], ..., A[curmax-1]
reverse(A.begin(), A.begin() + curmax);
// copy(A.begin(), A.end(), ostream_iterator<int>(cout, " "));
// cout << endl;
ans.push_back(curmax);
}
}
return ans;
}
};
//without actually flipping
//official solution
//time: O(N^2), space: O(N)
//Runtime: 8 ms, faster than 69.75% of C++ online submissions for Pancake Sorting.
//Memory Usage: 8.8 MB, less than 83.33% of C++ online submissions for Pancake Sorting.
class Solution {
public:
vector<int> pancakeSort(vector<int>& A) {
vector<int> ans;
int N = A.size();
vector<int> indices(N);
iota(indices.begin(), indices.end(), 1);
//the larget the former
sort(indices.begin(), indices.end(),
[&A](const int i, const int j){return A[i-1] > A[j-1];});
// copy(indices.begin(), indices.end(), ostream_iterator<int>(cout, " "));
// cout << endl;
for(int index : indices){
//don't actually reverse the vector, but simulate it
// cout << index << "->";
for(int flip : ans){
if(index <= flip){
//flip at flip'th position, so index'th position is affected
//old position + new position = flipped vector's size + 1
index = flip + 1 - index;
}
}
// cout << index << endl;
//now index becomes the true index after serveral flipping
ans.push_back(index);
ans.push_back(N--);
}
return ans;
}
};