알고리즘 문제풀이

스택 2 [백준 28278]

wiojfe 2024. 7. 9. 23:07

https://www.acmicpc.net/problem/28278

 

 

import sys 
input = sys.stdin.readline
test = int(input())
stack =[]
for _ in range(test):
    order = list(map(str,input().split()))
    # print(order)
    if order[0] == '4':
        # if len(stack)==0:print(1)
        print(1 if len(stack)==0 else 0 )
    elif order[0] == '1':
        stack.append(int(order[1]))
    elif order[0] == '2':
        if stack: print(stack.pop())
        else:print(-1)
    elif order[0] == '3':
        print(len(stack))
    else :
        if stack:print(stack[-1])
        else :print(-1)