본문 바로가기

알고리즘 문제풀이

스택 2 [백준 28278]

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)

'알고리즘 문제풀이' 카테고리의 다른 글

도키도키 간식드리미[백준 12789]  (1) 2024.07.11
풍선 터뜨리기[백준 2346]  (0) 2024.07.10
골드바흐 파티션[백준 17103]  (0) 2024.07.09
최소공배수 [백준 13241]  (0) 2024.07.09
분수 합 [백준 1735]  (1) 2024.07.09