[BOJ]10845:Q



🔒 例

>> 15
>> push 1
>> push 2
>> front
>> back
>> size
>> empty
>> pop
>> pop
>> pop
>> size
>> empty
>> pop
>> push 3
>> empty
>> front

1
2
2
0
1
2
-1
0
1
-1
0
3

🔧 に答える

1. n = int(sys.stdin.readline().rstrip())
2. for _ in range(n): sys.stdin.readline().split
3. queue: list 활용
	3.1 push: append() 
    3.2 pop: remove
    3.3 size : len()
    3.4 empty : len() = 0
    3.5 front : l[0]
    3.6 back : l[-1]

🔑 答案用紙

import sys

n = int(sys.stdin.readline().rstrip())
q = []

for _ in range(n):
    com = list(sys.stdin.readline().split())
    
    if com[0] == 'push':
        q.append(com[1])
        
    elif com[0] == 'pop':
        if len(q) == 0:
            print(-1)
            continue
        print(q[0])
        q.remove(q[0])

    elif com[0] == 'size':
        print(len(q))

    elif com[0] == 'empty':
        if len(q) == 0:
            print(1)
        else:
            print(0)

    elif com[0] == 'front':
        if len(q) == 0:
            print(-1)
            continue
        print(q[0])

    elif com[0] == 'back':
        if len(q) == 0:
            print(-1)
            continue
        print(q[-1])

💡 コンセプト

### queue: 큐
from queue import Queue

q = Queue()
q.put(4)	# push 4
q.put(5)	# push 5
q.get()		# pop -> 4
q.get()		# pop -> 5

### deque: 양방향 리스트
from collections import deque

dq = deque([4, 5, 6])
dq.append(7)	# push 7
dq.popleft()	# pop
len(dq)			# size, empty
dq[0]			# front	
dq[-1]			# back

dq.appendleft() # insert front