[알고리즘] 백준 1874 스택 수열

Song·2021년 6월 21일
0

알고리즘

목록 보기
13/22

문제링크

주제

  • 스택

난이도

  • 중상

풀이

import sys
from collections import deque

n = int(sys.stdin.readline())

stack = deque()
result = list()
answer = ""
max_num = 0

for _ in range(n):
    num = int(sys.stdin.readline().rstrip())

    i = max_num + 1     
    # stack 에 숫자 추가
    while i <= num:
        stack.append(i)
        result.append('+')
        # 이미 한번 추가된 숫자는 중복으로 쌓이지 않도록 확인해주는 변수
        if i > max_num: 
            max_num = i
        i += 1

    # 만약 마지막 값이 입력된 숫자와 동일하다면 pop() 진행
    if num == stack[-1]:
        stack.pop()
        result.append('-')
    else:
        answer = 'NO'
        break
    # count += 1

if answer == 'NO':
    print('NO')
else:
    for i in result:
        print(i)

문제를 풀고 알게된 개념 및 소감

profile
Learn From Yesterday, Live Today, Hope for Tomorrow

0개의 댓글