티스토리 뷰

Difficulty : Silver II

Problem Description

There is a stack and a sequence.

First, you can get an input N for the number of integers.

And you will get N lines of sequence. Each line has an integer and they never overlap.

You can push numbers from 1 to N into the stack and pop the top number.

You have to make sequence by listing the numbers that were pushed and popped.

If the sequence cannot be created in any way, the output will be 'NO'.

 

Solution

 

N is the number of integers.

stack means literally a stack.

top means the top number of the stack, and num means the next push number.

ans is for output, and b is for judging if the answer does not exists.

 

n is the element of sequence.

if the top number is bigger than n, it is impossible to make the sequence. So b is False.

Else if the top number is smaller than n, push numbers until the num is equal to n.

Add '+' in ans to every iteration.

After those processes, pop the stack to make the sequence and add add '-' in ans.

if the stack is empty, the top will be 0. Otherwise, the top will be the last number of stack.

Repeat this process N times.

 

If b is false, print 'NO'. Otherwise, print the '+', '-' which are in the ans.

 

Today's English

iteration : 반복

sequence : 수열

overlap : 중복되다

 

Today's Code

a.pop(), a.push() : 스택에서 사용되는 push, pop

 

여담

지금까지 풀었던 문제 중 가장 오답이 많이 나왔다.

IndexError는 금방 해결했지만 계속 7%에서 오답이 발생해서 한참을 끙끙댔는데

확실하진 않지만 마지막에 입력받는 정수들을 한 개씩 받아서 그때그때 처리하도록 하니 문제가 해결된 것을 보니

입력받는 정수들의 양이 문제였던 것 같다.

이전에는 정수를 for문으로 리스트 혹은 문자열 형태로 쭉 저장하고 그 다음 for문에서 매 index마다 불러서 문제를 해결했다.

지금까지 여러 입력이 들어오는 문제는 전부 이 방식대로 풀어 왔는데, 입력 양이 적어서 문제가 발생하지 않았던 것 같다.

오늘의 경험을 토대로 다음부터는 웬만하면 입력을 따로 저장하지 않는 방식으로 해결해 보겠다.

'프로그래밍 문제풀이 > 백준 문제풀이' 카테고리의 다른 글

1018번: 체스판 다시 칠하기  (0) 2022.10.22
1157번: 단어 공부  (0) 2022.10.22
1308번: D-Day  (0) 2022.10.19
1010번: 다리 놓기  (0) 2022.10.19
1271번: 엄청난 부자2  (0) 2022.10.17
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함