Skip to content

Commit 4c36386

Browse files
🎉 Day 29
1 parent 0ae13f3 commit 4c36386

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
## Week 5 🚧
4848

49-
Coming Soon...
49+
1. [Pancake Sort](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/553/week-5-august-29th-august-31st/3441/) ➡️ [CPP Solution](Week5/pancakeSort.cpp)
5050

5151
## Other Challenges 💪
5252

Week5/pancakeSort.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
private:
3+
int find(vector<int>& A, int target) {
4+
for(int i = 0; i < A.size(); ++i)
5+
if(A[i] == target) return i;
6+
return -1;
7+
}
8+
9+
void flip(vector<int>& A, int pos) {
10+
int i = 0;
11+
12+
while(i < pos) {
13+
int temp = A[i];
14+
A[i++] = A[pos];
15+
A[pos--] = temp;
16+
}
17+
}
18+
public:
19+
vector<int> pancakeSort(vector<int>& A) {
20+
vector<int> flips;
21+
22+
for(int n = A.size(); n > 0; --n) {
23+
int index = find(A, n);
24+
25+
flip(A, index);
26+
flip(A, n - 1);
27+
28+
flips.push_back(index + 1);
29+
flips.push_back(n);
30+
}
31+
32+
return flips;
33+
}
34+
};

0 commit comments

Comments
 (0)