백준 - 1874

Giho Kim·2023년 9월 18일

코테 연습

목록 보기
9/26

문제

내가 생각한 방법

  1. start = 0을 만들어줌 (중요)
  2. stack에 range(start, target + 1)을 넣어줌. operator에는 "+"
  3. pop을 해준뒤 해당 pop이 target과 같다면 operator에 "-"를 append해주며 start를 target + 1로 update해줌 (단 start가 target보다 적을때만)
  4. target과 같이 않다면 break후 NO
  5. 아니라면 계속 진행되며 operator에 있는 + - 를 print해준다
import sys

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

stack = []
operator = []
is_True = True
start = 1

for i in range(1, int(n) + 1):
  target = int(sys.stdin.readline())
  for j in range(start, target + 1):
    stack.append(j)
    operator.append("+")

  popped = stack.pop()
  if popped == target:
    operator.append("-")
    if target >= start:
        start = target + 1
  else:
    is_True = False
    break

if is_True and len(stack) == 0:
  for operate in operator:
    print(operate)
else:
  print('NO')

다른 사람 풀이

  • 비슷하긴 한데 cur이 start와 같은 역할을 함
n = int(input())
stack = []
answer = []
cur = 1

for i in range(n):
  num = int(input())

  while cur <= num:
    stack.append(cur)
    answer.append("+")
    cur += 1

  if num == stack.pop():
    answer.append('-')

  else:
    print('NO')
    break

if len(stack) == 0:
  for i in answer:
    print(i)
profile
취준돌이 개발자 김기호

0개의 댓글