개발/algorithm
[백준 10828번] 스택 - python
zzi_on2
2022. 2. 4. 10:38
요즘 계속 프로그래머스 문제들만 풀었는데 프로그래머스에는 상대적으로 스택을 사용하는 문제가 많이 없어서 그런지
간단한 스택 문제도 버벅거리면서 풀었다
이번 기회에 스택도 익혀두어야겠다
문제 링크
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
풀이
- 간단한 스택의 기본 함수들 구현 문제
import sys
input = sys.stdin.readline
n = int(input())
stack = []
for _ in range(n):
command = input().split()
if command[0] == 'push':
stack.append(int(command[1]))
elif command[0] =='pop':
if stack:
print(stack.pop())
else:
print(-1)
elif command[0] == 'size':
if stack:
print(len(stack))
else:
print(-1)
elif command[0] =='top':
if stack:
print(stack[-1])
else:
print(-1)
else:
if stack:
print(0)
else:
print(1)