백준#1874 스택 수열

정은경·2020년 11월 3일
0

백준 문제풀이

목록 보기
49/51

1. 문제

  • Easy / 스택,그리디 / 30분 컷


2. 나의 풀이

2-1. 풀이

자꾸 틀렸다는 나의 코드ㅠㅠ

import sys

n = int(sys.stdin.readline().split()[0])
target_list = []
for i in range(n):
    num = int(sys.stdin.readline().split()[0])
    target_list.append(num)

print_list = []
stack_list = []
is_sequence = True
nums = [x for x in range(1, n+1)]
while target_list:
    target = target_list.pop(0)

    if stack_list and stack_list[-1] == target:
        stack_list.pop()
        print_list.append("-")
    else: # stack_list가 비었거나 stack_list[-1]이 target과 다를 때 push를 합니다
        if not nums:
            is_sequence = False
            break
        stack_list.append(nums.pop(0))
        print_list.append("+")

if is_sequence:
    for p in print_list:
        print(p)
else:
    print("NO")

3. 남의 풀이

n = int(input())
count = 1
stack = []
result = []

for i in range(1, n+1):
    data = int(input())
    while count <= data:
        stack.append(count)
        count += 1
        result.append('+')
    if stack[-1] == data:
        stack.pop()
        result.append('-')
    else:
        print('No')
        exit(0)

print('\n'.join(result))

4. 느낀 점

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글