Build an Array With Stack Operations LeetCode solution in C++

1
class Solution {
2
public:
3
vector<string> buildArray(vector<int> &target, int n) {
4
vector<string> ans;
5
int l = target.size(), count = 0,
6
ind = 0; // ind is index of the target array
7

8
for (int i = 1; i <= n; i++) {
9
if (count == l) break;
10
if (target[ind] != i) {
11
ans.push_back("Push");
12
ans.push_back("Pop");
13
} else {
14
ans.push_back("Push");
15
count++;
16
ind++;
17
}
18
}
19
return ans;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0