-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile1.js
More file actions
34 lines (33 loc) · 1001 Bytes
/
Copy pathfile1.js
File metadata and controls
34 lines (33 loc) · 1001 Bytes
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
33
34
function solution(n, k, cmd) {
let res = [];
let stack = []; //erased rows
for(let i = 0; i<n;i++){
res.push('O'); //initialize
}
for(let i = 0; i<cmd.length;i++){
if(cmd[i].length >1){
if(cmd[i][0] == "U"){
let Xnum = res.slice(0, k).filter(a => a == 'X').length;
k -= (parseInt(cmd[i][2]) + Xnum);
}else if(cmd[i][0] == "D"){
let Xnum = res.slice(k+1).filter(a => a == 'X').length;
k += (parseInt(cmd[i][2]) + Xnum);
}
}else{
if(cmd[i] == "C"){
stack.push(k);
res[k] = 'X';
if(k !== n-1){
k++;
}else k--;
}else if(cmd[i] == "Z"){
let deleted = stack.pop();
res[deleted] = "O";
if(deleted < k) k++;
}
}
console.log(cmd[i],res,k);
}
return res.join('');
}
console.log(solution(5,0, ["C","Z"]));