티스토리 뷰
https://www.acmicpc.net/problem/10828
출력. console.log 때문에 시간초과가 발생했다.
console.log로 한번에 출력하는 연습을 해야겠다.
const fs = require('fs');
const [n, ...arr] = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
class Node{
constructor(item){
this.item = item;
this.next = null;
}
}
class Stack{
constructor(){
this.topOfStack = null;
this.length = 0;
}
push(item){
const node = new Node(item);
if(this.topOfStack!=null){
node.next = this.topOfStack;
}
this.topOfStack = node;
this.length+=1;
}
pop(){
if(this.length==0)return -1;
const popItem = this.topOfStack;
this.topOfStack = popItem.next;
this.length-=1;
return popItem.item
}
size(){
return this.length;
}
empty(){
if(this.length==0) return 1;
else return 0;
}
top(){
if(this.length==0)return -1;
return this.topOfStack.item;
}
}
let answer = [];
let stack = new Stack();
const command = arr.map(v=>v.split(' '));
command.forEach(v=>{
switch(v[0]){
case 'push':
stack.push(v[1])
break;
case 'pop':
answer.push(stack.pop());
break;
case 'size':
answer.push(stack.size())
break;
case 'empty':
answer.push(stack.empty())
break;
case 'top':
answer.push(stack.top())
break;
}
})
console.log(answer.join('\n'))
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 9012번 : 괄호 (0) | 2021.09.13 |
---|---|
Node.js) 백준 10773번 : 제로 (0) | 2021.09.13 |
Node.js) 백준 12789번 : 도키도키 간식드리미 (0) | 2021.09.13 |
Node.js) 백준 2004번 : 조합 0의 개수 ★ (0) | 2021.09.13 |
Node.js) 백준 1676번 : 팩토리얼 0의 계수 (0) | 2021.09.13 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DB 생성
- 동적프로그래밍
- MySQL
- BFS
- 은둔청년체험
- 투포인터 연습
- 최소공통조상
- 투포인터
- node.js
- 다이나밍프로그래밍
- 개발자면접
- 그래프
- create db
- 면접비
- 다이나믹프로그래밍
- create databases;
- 서버점검
- 면접질문
- 로드나인
- MOD
- 서버개발
- 롱베케이션
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함