본문 바로가기
python-algorithm

백준 10866 덱

by 무적김두칠 2021. 8. 24.

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
from collections import deque
import sys
ans=deque()
n=int(input())
for i in range(n):
    tmp=list(sys.stdin.readline().split())
    if tmp[0]=='push_back' : ans.append(int(tmp[1]))
    if tmp[0== 'push_front': ans.appendleft(int(tmp[1]))
    if tmp[0]=='front' :
        if len(ans)==0print(-1)
        elseprint(ans[0])
    if tmp[0]=='back' :
        if len(ans)==0print(-1)
        elseprint(ans[-1])
    if tmp[0]=='size'print(len(ans))
    if tmp[0]=='empty':
        if len(ans)==0 : print(1)
        else :print (0)
    if tmp[0]=='pop_front':
        if len(ans)==0print (-1)
        else:
            print(ans[0])
            ans.popleft()
    if tmp[0]=='pop_back':
        if len(ans)==0print (-1)
        else:
            print(ans[-1])
            ans.pop()
cs

pop_back, pop_front는 deque안에 함수 popleft , pop 이용하시면 됩니다

이런 자료구조들이 처음이시거나개념 자체가 이해가 안되시면
구글링 해보시고 다른 언어들은 어떻게 하고있나 보시면 더 좋을것 같아요

반응형

'python-algorithm' 카테고리의 다른 글

백준 10773 제로  (0) 2021.08.24
백준 11651 좌표 정렬하기 2  (0) 2021.08.24
백준 10845 큐  (0) 2021.08.24
백준 10824 스택  (0) 2021.08.24
백준 2164 카드 2  (0) 2021.08.24

댓글