💡 파이썬에서는 stack을 list로 구현하고 메소드도 모두 지원한다.

import sys
input = sys.stdin.readline
stack = []
N = int(input())
for _ in range(N):
command = input().rstrip().split(' ')
if command[0] == "push":
stack.append(command[1])
elif command[0] == "top":
if stack:
print(stack[-1])
else:
print("-1")
elif command[0] == "size":
print(len(stack))
elif command[0] == "empty":
if stack:
print("0")
else:
print("1")
elif command[0] == "pop":
if stack:
print(stack.pop())
else:
print("-1")
- 비교적으로 쉬운 문제였다. stack의 pop(), append(), len()등만 사용해서 쉽게 구현할 수 있는 문제였고, 문제에서 지시한대로 조건문을 걸어서 진행하였다.
- 시간초과가 0.5초가 존재했기 때문에 입력받는 경우가 많으니 input()으로 받지 않고, sys.stdin.readlin()으로 입력 시간을 줄였다.