💡문제접근
- 명령 길이에 대해서 경우를 나눈 다음 각각의 명령을 코드로 작성했다.
- sys를 사용하지 않으면 시간초과가 발생했다. sys를 사용했더니 정상적으로 출력되었다.
💡코드(메모리 : 30748KB, 시간: 56ms)
import sys
N = int(input())
stack = []
for _ in range(N):
command = list(map(str, sys.stdin.readline().strip().split()))
if len(command) == 2:
if command[0] == "push":
stack.append(int(command[1]))
else:
if command[0] == "pop":
if len(stack) == 0:
print(-1)
else:
temp = stack.pop()
print(temp)
elif command[0] == "size":
print(len(stack))
elif command[0] == "empty":
if len(stack) == 0:
print(1)
else:
print(0)
elif command[0] == "top":
if stack == []:
print(-1)
else:
print(stack[-1])
💡소요시간 : 7m