[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)
'Problem Solving > BAEKJOON' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[baekjoon]python #1966 ํ๋ฆฐํฐ ํ (0) | 2022.06.23 |
---|---|
[baekjoon]python #2805 ๋๋ฌด ์๋ฅด๊ธฐ (0) | 2022.06.23 |
[baekjoon]python #10773 ์ ๋ก (0) | 2022.06.23 |
[baekjoon]python #10816 ์ซ์ ์นด๋ 2 (0) | 2022.06.03 |
[baekjoon]python #1920 ์ ์ฐพ๊ธฐ (0) | 2022.06.03 |
๋๊ธ