반응형
import sys
n = int(input())
stack = []
for i in range(n):
cmd = list(map(int, sys.stdin.readline().split()))
if cmd[0] == 1:
stack.append(cmd[1])
elif cmd[0] == 2:
if stack:
print(stack.pop())
else:
print(-1)
elif cmd[0] == 3:
print(len(stack))
elif cmd[0] == 4:
if stack:
print(0)
else:
print(1)
else:
if len(stack) > 0:
print(stack[-1])
else:
print(-1)
하라는대로 하기
파이썬의 list pop(), append() 시간복잡도는 O(1)
💡 파이썬 리스트가 비어있는지 체크하는 방법
if stack:
print(0)
else:
print(1)
반응형
'취준 > 코딩테스트' 카테고리의 다른 글
백준 9012 (python): 괄호 (0) | 2024.06.19 |
---|---|
백준 10773 (python): 제로 (1) | 2024.06.18 |
백준 1292 (python): 쉽게 푸는 문제 (0) | 2024.04.11 |
백준 2581 (python): 소수 (0) | 2024.04.11 |
백준 1406 (python): 에디터 (0) | 2024.04.11 |