Skip to content

Commit aca4843

Browse files
committed
[Silver IV] Title: 스택, Time: 104 ms, Memory: 12280 KB -BaekjoonHub
1 parent 77e42f2 commit aca4843

2 files changed

Lines changed: 44 additions & 35 deletions

File tree

백준/Silver/10828. 스택/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 34052 KB, 시간: 72 ms
7+
메모리: 12280 KB, 시간: 104 ms
88

99
### 분류
1010

11-
자료 구조, 구현, 스택
11+
구현, 자료 구조, 스택
1212

1313
### 제출 일자
1414

15-
2024년 4월 11일 13:36:23
15+
2026년 3월 4일 13:06:26
1616

1717
### 문제 설명
1818

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
// dev/stdin
2-
// ./input.txt
3-
const array = require('fs').readFileSync('dev/stdin').toString().split('\n');
4-
5-
const stack = [];
6-
const result = [];
7-
8-
const len = array.shift();
9-
10-
for (let i = 0; i < len; i++) {
11-
switch(array[i]) {
12-
case 'pop':
13-
result.push(stack.pop() || -1);
14-
break;
15-
16-
case 'size':
17-
result.push(stack.length);
18-
break;
19-
20-
case 'empty':
21-
result.push(stack[0] ? 0 : 1);
22-
break;
23-
24-
case 'top':
25-
result.push(stack[stack.length - 1] || -1);
26-
break;
27-
28-
default:
29-
stack.push(array[i].split(" ")[1]);
30-
break;
31-
}
1+
// /dev/stdin
2+
const input = require("fs")
3+
.readFileSync("/dev/stdin")
4+
.toString()
5+
.trim()
6+
.split("\n")
7+
.map((x) => x.replace("\r", ""));
8+
9+
const N = Number(input[0]);
10+
const STACK = [];
11+
let answer = "";
12+
13+
for (let i = 1; i <= N; i++) {
14+
const cmd = input[i];
15+
16+
if (cmd.startsWith("push")) {
17+
const val = Number(cmd.split(" ")[1]);
18+
STACK.push(val);
19+
continue;
20+
}
21+
22+
if (cmd === "pop") {
23+
answer += (STACK.length ? STACK.pop() : -1) + "\n";
24+
continue;
25+
}
26+
27+
if (cmd === "size") {
28+
answer += STACK.length + "\n";
29+
continue;
30+
}
31+
32+
if (cmd === "empty") {
33+
answer += (STACK.length === 0 ? 1 : 0) + "\n";
34+
continue;
35+
}
36+
37+
if (cmd === "top") {
38+
answer += (STACK.length > 0 ? STACK[STACK.length - 1] : -1) + "\n";
39+
continue;
40+
}
3241
}
3342

34-
console.log(result.join('\n'));
43+
console.log(answer);

0 commit comments

Comments
 (0)