알고리즘

<백준> 10828번 파이썬 알고리즘

changha. 2021. 7. 11. 15:54
import sys

n = int(sys.stdin.readline())
stk = []
for _ in range(n):
  m = sys.stdin.readline().split()

  if m[0] == 'push':
      stk.append(m[1])
  elif m[0] == "pop":
      if len(stk) != 0:
        print(stk.pop())
      else:
        print(-1)
  elif m[0] == "size":
    print(len(stk))
  elif m[0] == "top":
    if len(stk) != 0:
        print(stk[-1])
    else:
        print(-1)
  elif m[0] == 'empty':
    if len(stk) != 0:
      print(0)
    else:
      print(1)