-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPancake-Sorting.cpp
32 lines (31 loc) · 940 Bytes
/
Pancake-Sorting.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
class Solution
{
public:
vector<int> pancakeSort(vector<int> &arr)
{
vector<int> ans;
for (int i = 1; i < arr.size(); i++)
{
if (arr[i] > arr[i - 1])
continue;
else if (arr[i] < arr[i - 1])
{
int idx = upper_bound(arr.begin(), arr.begin() + i, arr[i]) - arr.begin();
if (idx - 1 + 1 > 1)
ans.push_back(idx - 1 + 1);
if (i - 1 + 1 > 1)
ans.push_back(i - 1 + 1);
if (i + 1 > 1)
ans.push_back(i + 1);
if (idx + 1 > 1)
ans.push_back(idx + 1);
for (int j = i - 1; j >= 0; j--)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
else
break;
};
};
return ans;
}
};