-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack 평가.js
54 lines (42 loc) · 979 Bytes
/
stack 평가.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* 기차 운행 */
if(! Array.prototype.peak){
Array.prototype.peak = function(){
return this[this.length-1];
};
}
if(! Array.prototype.isEmpty){
Array.prototype.isEmpty = function(){
return this.length === 0;
}
};
/* user code */
function answer(train) {
// 코드 구현 시작 영역
let stack = [],
num = 0;
for(let i = 0 ; i < train.length; i++){
while(stack.isEmpty() || stack.peak() < train[i]){
stack.push(++num);
};
if(stack.peak() === train[i]){
stack.pop();
}else {
return false;
}
}
return true;
// 코드 구현 종료 영역
}
/* main code */
let input = [
// TC: 1
[1, 2, 3],
// TC: 2
[3, 2, 1],
// TC: 3
[3, 1, 2],
];
for (let i = 0; i < input.length; i++) {
process.stdout.write(`#${i + 1} `);
console.log(answer(input[i]));
}