๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/BAEKJOON

[baekjoon]python #1874 ์Šคํƒ ์ˆ˜์—ด

by DevIseo 2022. 6. 23.

[baekjoon]python #1874 ์Šคํƒ ์ˆ˜์—ด

https://www.acmicpc.net/problem/1874

 

1874๋ฒˆ: ์Šคํƒ ์ˆ˜์—ด

1๋ถ€ํ„ฐ n๊นŒ์ง€์— ์ˆ˜์— ๋Œ€ํ•ด ์ฐจ๋ก€๋กœ [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ˆ˜์—ด [4, 3, 6, 8, 7, 5, 2, 1]์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net

 

 

์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ์ข€ ์–ด๋ ค์› ๋‹ค.

๊ทธ๋ž˜์„œ ํ‘œ๋กœ ํ•˜๋‚˜์”ฉ ๊ทธ๋ ค๋ณด๋ฉด์„œ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๊ตฟํ˜„ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

1. ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ์˜ฌ๋ ค๊ฐ€๋ฉฐ target์ˆซ์ž์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ stack์— ๋„ฃ์–ด์ค€๋‹ค.

2. target๊ณผ stack์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์ˆ˜๊ฐ€ ๊ฐ™์„ ๋•Œ pop()์‹œ์ผœ์ค€๋‹ค.

3.๋งŒ์•ฝ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ NO๋ฅผ ์ถœ๋ ฅํ›„ break์‹œํ‚ค๋ฉด ๋œ๋‹ค!

n = int(input())
stack=[]
result=[]
flag=0
cNum=1

for i in range(n):
    target=int(input())
    while cNum<=target:
        stack.append(cNum)
        result.append("+")
        cNum+=1

    if stack[-1] == target:
        stack.pop()
        result.append("-")

    else:
        print("NO")
        flag=1
        break

if flag==0:
    for i in result:
        print(i)

๋Œ“๊ธ€