-
[백준] 1874 스택 수열백준 Online Judge 2021. 8. 25. 20:33
스택 수열
- 유형: 스택, 그리디
- 난이도: 하
- 스택에 push 할 때는 그 숫자를 넣을 때까지 삽입하면 된다. (예를 들어, 4를 넣고자 하면 1, 2, 3, 4를 넣는다).
- 스택에서 pop 하는 경우, 내림차순으로 빼낼 수 있는지 확인해야 한다.
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
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))
GitHub - DAWUNHAN/Algorithms-and-DataStructure: Algorithms and DataStructure with Python
Algorithms and DataStructure with Python. Contribute to DAWUNHAN/Algorithms-and-DataStructure development by creating an account on GitHub.
github.com
[ 패스트캠퍼스 알고리즘 / 기술면접 완전 정복 올인원 패키지 Online ] 강의 내용을 정리한 자료입니다.
'백준 Online Judge' 카테고리의 다른 글
[백준] 1920 수 찾기 (0) 2021.08.26 [백준] 10930 SHA-256 (0) 2021.08.26 [백준] 5397 키로그 (0) 2021.08.25 [백준] 2798 블랙잭 (0) 2021.08.23 [배열] 하 - 2920 음계 문제 (0) 2021.08.23