[알고리즘/백준] 10828: 스택(python)

유현민·2022년 4월 4일
0

알고리즘

목록 보기
88/253

모든 명령을 함수로 구현했다. 처음에 input()으로 입력을 받으니 시간초과가 나서 stdin.readline()으로 받으니 해결

from collections import deque
from sys import stdin

def push(x):
    stack.append(x)


def pop():
    if len(stack) == 0:
        print(-1)
    else:
        print(stack.pop())


def size():
    print(len(stack))


def empty():
    if len(stack) == 0:
        print(1)
    else:
        print(0)


def top():
    if len(stack) == 0:
        print(-1)
    else:
        print(stack[-1])


N = int(input())
stack = deque()
for i in range(N):
    a = stdin.readline().split()
    if "push" in a:
        push(a[1])
    elif "pop" in a:
        pop()
    elif "size" in a:
        size()
    elif "empty" in a:
        empty()
    else:
        top()
profile
smilegate megaport infra

0개의 댓글