문제 확인하기
2. 정답 코드 (Python, memory: 30840KB, time: 76ms)
from sys import stdin
N = int(stdin.readline())
stack = []
for _ in range(N):
command = stdin.readline().split()
order = command[0]
if order == "push":
result = command[1]
stack.append(result)
elif order == "pop":
if len(stack) == 0:
print(-1)
else:
print(stack.pop())
elif order == "size":
print(len(stack))
elif order == "empty":
if len(stack) == 0:
print(1)
else:
print(0)
elif order == "top":
if len(stack) == 0:
print(-1)
else:
print(stack[-1])