๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 18258 - ํ

by YWTechIT 2021. 5. 4.
728x90

๐Ÿ“Œ ๋ฐฑ์ค€ 18258 - ํ

๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก ๋‚˜์˜ ํ’€์ด

๋ฐฑ์ค€ 10773 - ์ œ๋กœ ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ์ง€๋งŒ front, back์˜ ๊ฒฝ์šฐ ๊ทธ๋ฆฌ๊ณ  pop์—์„œ ์ œ์ผ ์•ž์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ๋นผ๋Š” ๊ฒฝ์šฐ๋งŒ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์ œ์ผ ์•ž์— ์žˆ๋Š” ๊ฐ’์„ ๋นผ๋‚ผ ๋•Œ์—๋Š” deque๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œํ‚ค๋Š” ๊ฒƒ ์žŠ์ง€ ๋ง์ž!

 

from collections import deque
import sys
input = sys.stdin.readline

n = int(input())
stack = deque([])

def push(x):
    stack.append(x)

def pop():
    if not stack:
        return -1
    return stack.popleft()

def size():
    return len(stack)

def empty():
    if not stack:
        return 1
    return 0

def front():
    if not stack:
        return -1
    return stack[0]

def back():
    if not stack:
        return -1
    return stack[-1]

for _ in range(n):
    command = input().split()
    if 'push' in command:
        push(command[1])
    elif 'front' in command:
        print(front())
    elif 'back' in command:
        print(back())
    elif 'size' in command:
        print(size())
    elif 'empty' in command:
        print(empty())
    else:
        print(pop())
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€