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

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

by YWTechIT 2021. 5. 5.
728x90

๐Ÿ“Œ ๋ฐฑ์ค€ 10866 - ๋ฑ

๋ฌธ์ œ ์„ค๋ช…


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

๋ฐฑ์ค€ 10828 - ์Šคํƒ๊ณผ ๋ฐฑ์ค€ 18258 - ํ์™€ ๊ฐ™์€ ์œ ํ˜•์ด๋ฉด์„œ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ๋ฑ ๋ฌธ์ œ๋‹ค.

 

์ด๋ฒˆ์—๋Š” ์ด์ „์— ๊ณต๋ถ€ํ–ˆ๋˜ ๋‚ด์šฉ์ธ ๋‹จ๋ฝํ‰๊ฐ€๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๋Š”๋ฐ ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๊ฐ„๊ฒฐํ•ด์ง„๊ฒƒ ๊ฐ™๋‹ค. ๋‹จ๋ฝํ‰๊ฐ€๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์งง๊ฒŒ ๊ตฌํ˜„ํ•ด๋ณด๋Š” ๋ฐฉ๋ฒ•๋„ ์ตํ˜€๋‘์ž.

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

def push_front(x):
    stack.appendleft(x)

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

def pop_front():
    return stack and stack.popleft() or -1

def pop_back():
    return stack and stack.pop() or -1

def size():
    return len(stack)

def empty():
    return not stack and 1 or 0

def front():
    return stack and stack[0] or -1

def back():
    return stack and stack[-1] or -1

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

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

๋Œ“๊ธ€