forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
37 lines (27 loc) · 734 Bytes
/
main.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
/// Source : https://leetcode.com/problems/pancake-sorting/
/// Author : liuyubobobo
/// Time : 2018-01-05
#include <iostream>
#include <vector>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(n^2)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> pancakeSort(vector<int>& A) {
int n = A.size();
vector<int> res;
for(int i = n - 1; i >= 0; i --){
int max_index = find(A.begin(), A.end(), i + 1) - A.begin();
res.push_back(max_index + 1);
reverse(A.begin(), A.begin() + max_index + 1);
res.push_back(i + 1);
reverse(A.begin(), A.begin() + i + 1);
}
return res;
}
};
int main() {
return 0;
}