ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

https://www.acmicpc.net/problem/10828

 

10828๋ฒˆ: ์Šคํƒ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

 

๋ฌธ์ œํ’€์ด

๊ฐ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ์ฝ˜์†”์„ ์ฐ์–ด์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋ฉด ํƒ€์ž„ ์˜ค๋ฒ„๋กœ ์ธํ•ด ์˜ค๋‹ต์ด ๋œ๋‹ค.์ด์— stack๊ณผ answer์— ์šฐ์„ ์ ์œผ๋กœ ํ•ด๋‹น ๋ช…๋ น์–ด๋“ค์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•ด์ฃผ๋ฉฐ ์ตœ์ข…์ ์ธ ๊ฐ’์„ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•ด์ค˜์•ผ ๋œ๋‹ค.

 

์ฝ”๋“œ

const array = require('fs').readFileSync('/dev/stdin').toString().split('\n');

const answer = [];
const stack = [];

array.shift();

for(let x of array) {
    switch(x) {
        case "pop":
            answer.push(stack.pop() || -1);
            break;
        case "size":
            answer.push(stack.length);
            break;
        case "empty":
            stack.length === 0 ? answer.push(1) : answer.push(0);
            break;
        case "top":
            answer.push(stack[stack.length - 1] || -1);
            break;
        default:
            stack.push(x.split(' ')[1]);
            break;
    }
}

console.log(answer.join('\n'));
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ