백준 1874 스택 수열

Hyun·2022년 10월 8일
0

코딩테스트

목록 보기
10/66

https://www.acmicpc.net/problem/1874
실패이유 : 시간초과

import sys

N = int(input())
stack = []
i = 1
ins = []
flag = True
for _ in range(N):
    n = int(sys.stdin.readline().rstrip())

    while i <= n:
        stack.append(i)
        ins.append("+")
        i += 1

    if stack[-1] == n:
        stack.pop()
        ins.append("-")
    else:
        flag = False

if flag is True:
    for k in ins:
        print(k)
else:
    print("NO")
  • 주어진 숫자가 나올 때 까지 stack 에 push
  • 만약 주어진 숫자가 stack 의 top 부분과
    • 같으면 ➔ stack.pop()
    • 다르면 ➔ 만들 수 없는 수열
  • 만들 수 없는 수열이면 NO 출력
  • 만들 수 있는 수열이면 ins 에 저장된 연산기호들 모두 출력

출처 : https://hongcoding.tistory.com/39

0개의 댓글

관련 채용 정보