import sys
input = sys.stdin.readline
stack = []
for _ in range(int(input())):
commands = input().rstrip().split()
if commands[0] == "push": stack.append(int(commands[1]))
elif commands[0] == "pop": print(-1) if not stack else print(stack.pop())
elif commands[0] == "size": print(len(stack))
elif commands[0] == "empty": print(0) if stack else print(1)
elif commands[0] == "top": print(stack[-1] if stack else -1)
스택에 대한 아주 기초적인 알고리즘 문제다.
Learned
- print문 안에서 조건문을 쓰면 특정 조건이 만족했을 때 값을 print 해준다
'Algorithm > BaekJoon' 카테고리의 다른 글
[백준/Python] 2573번 - 빙산 (0) | 2023.09.06 |
---|---|
[백준/Python] 2170 - 선 긋기 (0) | 2023.09.02 |
[백준/Python] 1874번 - 스택 수열 (1) | 2023.09.02 |
[백준/Python] 2493번 - 탑 (0) | 2023.09.01 |
[백준/Python] 15903번 - 카드 합체 놀이 (0) | 2023.09.01 |