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())
๋ฐ์ํ
'Algorithm > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ํ์ด์ฌ(python) ] ๋ฐฑ์ค 1406 - ์๋ํฐ (0) | 2021.05.10 |
---|---|
[ ํ์ด์ฌ(python) ] ๋ฐฑ์ค 2693 - N๋ฒ์งธ ํฐ ์ (0) | 2021.05.07 |
[ ํ์ด์ฌ(python) ] ๋ฐฑ์ค 18258 - ํ (0) | 2021.05.04 |
[ ํ์ด์ฌ(python) ] ๋ฐฑ์ค 10828 - ์คํ (0) | 2021.05.04 |
[ ํ์ด์ฌ(python) ] ๋ฐฑ์ค 10773 - ์ ๋ก (0) | 2021.05.04 |
๋๊ธ