반응형
문제
https://www.acmicpc.net/problem/10828
풀이
class Stack {
constructor() {
this.stack = [];
}
push(newElement) {
this.stack.push(newElement);
}
pop() {
if (this.empty()) return -1;
return this.stack.pop();
}
size() {
return this.stack.length;
}
empty() {
return this.stack.length === 0 ? 1 : 0;
}
top() {
if (this.empty()) return -1;
return this.stack.at(-1);
}
}
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const [n, ...arr] = fs.readFileSync(filePath).toString().trim().split("\n");
const commands = arr.map((command) => command.split(" "));
const stack = new Stack();
let answer = [];
for (let [com, num] of commands) {
if (com === "push") {
stack.push(num);
} else {
answer.push(stack[com]());
}
}
console.log(answer.join("\n"));
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[알고리즘/백준/9012] 괄호(스택) (0) | 2023.05.25 |
---|---|
[알고리즘/백준/14940] 쉬운 최단거리(BFS, Nodejs) (1) | 2023.05.24 |
[알고리즘/백준/16918] 봄버맨(BFS, Nodejs) (4) | 2023.05.23 |
[알고리즘/백준/2667] 단지 번호 붙이기 (1) | 2023.05.23 |
[알고리즘/백준/1325] 효율적인 해킹(BFS, JavaScript, 백준 Nodejs로 풀 때 유의사항) (4) | 2023.05.22 |