Skip to content

Commit 65c0bbc

Browse files
authored
Create build-an-array-with-stack-operations.cpp
1 parent e7d6b62 commit 65c0bbc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<string> buildArray(vector<int>& target, int n) {
7+
vector<string> result;
8+
int curr = 1;
9+
for (const auto& t : target) {
10+
for (int i = curr; i < t; ++i) {
11+
result.emplace_back("Push");
12+
result.emplace_back("Pop");
13+
}
14+
result.emplace_back("Push");
15+
curr = t + 1;
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)