백준_스택_스택_10828_파이썬

석준·2022년 8월 31일
0

백준_문제풀이

목록 보기
26/30
post-thumbnail

✅문제 요약

  1. n개의 명령
  2. 해당 명령을 따라 스택을 작동시킨다.
    1) push: push / pop: pop후 출력 / top: Last input 출력 / size: 스택 크기 출력 / empty: 비어있다면 1 아니면 0

✅문제 풀이

난 스택의 최대크기를 명령의 크기만큼 미리 만들고 top을 인덱스이동으로 구현하였다.

import sys
input = sys.stdin.readline

n = int(input())
arr = [-1]*(n+1)
top = -1

for i in range(n):
    comm = input().rstrip()
	
    # 뒤에 숫자가 오는 명령 == push
    if comm[-1].isdigit():
        tmp, num = comm.split()
        top += 1
        arr[top] = num
	
    # pop / size / top / empty
    else:
        if comm == 'pop':
            if top == -1:
                print(-1)
            else:
                print(arr[top])
                top -= 1
        elif comm == 'size':
            print(top+1)
        elif comm == 'empty':
            print(1 if top == -1 else 0)
        else:       # top
            print(arr[top])
profile
파이썬 서버 개발자 지망생

0개의 댓글