Skip to content

Commit f6f52d0

Browse files
authored
Create 1441-build-an-array-with-stack-operations.js
1 parent de52b14 commit f6f52d0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} target
3+
* @param {number} n
4+
* @return {string[]}
5+
*/
6+
const buildArray = function(target, n) {
7+
const res = []
8+
let ti = 0, ni = 1, num = 0
9+
while(num !== target.length && ni <= n) {
10+
if(ni !== target[ti]) {
11+
res.push('Push', 'Pop')
12+
ni++
13+
}else {
14+
res.push('Push')
15+
ni++
16+
num++
17+
ti++
18+
}
19+
}
20+
21+
return res
22+
};

0 commit comments

Comments
 (0)