forked from fastcampus-team4/Algorithm-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10866.js
70 lines (67 loc) · 1.59 KB
/
10866.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// https://www.acmicpc.net/problem/10866
const filePath = process.platform === 'linux' ? 0 : 'input.txt';
const input = require('fs').readFileSync(filePath).toString().split('\n');
const N = Number(input.shift());
class Deque {
constructor() {
this._arr = [];
}
push_front(data) {
this._arr.unshift(data);
}
push_back(data) {
this._arr.push(data);
}
size() {
return this._arr.length;
}
pop_front() {
return this.size() === 0 ? -1 : this._arr.shift();
}
pop_back() {
return this.size() === 0 ? -1 : this._arr.pop();
}
empty() {
return this.size() === 0 ? 1 : 0;
}
front() {
return this.size() === 0 ? -1 : this._arr[0];
}
back() {
return this.size() === 0 ? -1 : this._arr[this.size() - 1];
}
}
function solution(n, input) {
const deque = new Deque();
let result = [];
for (let i = 0; i < n; i++) {
let command = input[i].split(' ')[0];
switch (command) {
case 'push_back':
deque.push_back(Number(input[i].split(' ')[1]));
break;
case 'push_front':
deque.push_front(Number(input[i].split(' ')[1]));
break;
case 'pop_front':
result.push(deque.pop_front());
break;
case 'pop_back':
result.push(deque.pop_back());
break;
case 'size':
result.push(deque.size());
break;
case 'empty':
result.push(deque.empty());
break;
case 'front':
result.push(deque.front());
break;
case 'back':
result.push(deque.back());
}
}
return result.join('\n');
}
console.log(solution(N, input));