ps:problems:boj:10866
                덱
| ps | |
|---|---|
| 링크 | acmicpc.net/… | 
| 출처 | BOJ | 
| 문제 번호 | 10866 | 
| 문제명 | 덱 | 
| 레벨 | 실버 4 | 
| 분류 | 
 덱  | 
	
| 시간복잡도 | O(n) | 
| 인풋사이즈 | n<=10000 | 
| 사용한 언어 | Python | 
| 제출기록 | 32084KB / 120ms | 
| 최고기록 | 64ms | 
| 해결날짜 | 2021/08/21 | 
풀이
- 파이썬에서는 collections.deque를 이용해서 주어진 연산들을 처리해주면 된다
 - 주어진 연산들은 덱에서 모두 O(1)에 처리되므로 총 시간복잡도는 O(n).
 
코드
"""Solution code for "BOJ 10866. 덱".
- Problem link: https://www.acmicpc.net/problem/10866
- Solution link: http://www.teferi.net/ps/problems/boj/10866
Tags: [deque]
"""
import collections
import sys
def main():
    N = int(sys.stdin.readline())
    deq = collections.deque()
    for _ in range(N):
        oper = sys.stdin.readline().split()
        op = oper[0]
        if op == 'push_front':
            deq.appendleft(oper[1])
        elif op == 'push_back':
            deq.append(oper[1])
        elif op == 'pop_front':
            print(deq.popleft() if deq else '-1')
        elif op == 'pop_back':
            print(deq.pop() if deq else '-1')
        elif op == 'size':
            print(len(deq))
        elif op == 'empty':
            print('0' if deq else '1')
        elif op == 'front':
            print(deq[0] if deq else '-1')
        elif op == 'back':
            print(deq[-1] if deq else '-1')
if __name__ == '__main__':
    main()
ps/problems/boj/10866.txt · 마지막으로 수정됨: 2021/10/15 16:22 저자 teferi
                
                
토론