<오답>
import sys
input = sys.stdin.readline
def push(x):
return queue.append(x)
def pop():
if len(queue) > 0:
return queue.pop(0)
return -1
def size():
return len(queue)
def empty():
if len(queue) > 0:
return 0
return 1
def front():
if len(queue) > 0:
return queue[0]
return -1
def back():
if len(queue) > 0:
return queue[-1]
return -1
n = int(input())
queue = []
for _ in range(n):
command = input().split()
if command[0] == "push":
push(command[1])
elif command[0] == "pop":
print(pop())
elif command[0] == "size":
print(size())
elif command[0] == "empty":
print(empty())
elif command[0] == "front":
print(front())
else:
print(back())
#시간초과
<정답>
from collections import deque
import sys
input = sys.stdin.readline
n = int(input())
queue = deque()
for _ in range(n):
command = input().split()
if command[0] == 'push':
queue.append(command[1])
elif command[0] == 'pop':
if len(queue)>0:
print(queue.popleft())
else:
print(-1)
elif command[0] == 'size':
print(len(queue))
elif command[0] == 'empty':
if len(queue) == 0:
print(1)
else:
print(0)
elif command[0] == 'front':
if queue:
print(queue[0])
else:
print(-1)
elif command[0] == 'back':
if queue:
print(queue[-1])
else:
print(-1)
#데크를 사용하여 해결 했다.
#데크 사용 없이도 풀이 가능한 문제임.
#pop 함수때문인지 위의 풀이는 틀림
'문제풀이 > 백준(Boj) 문제풀이' 카테고리의 다른 글
[백준][BFS] 1753. 최단경로 (파이썬/Python) (0) | 2021.11.09 |
---|---|
[백준][BFS] 7576. 토마토 (파이썬/Python) (0) | 2021.11.09 |
[백준][구현] 2504.괄호의 값 (파이썬/Python) (0) | 2021.11.02 |
[백준][구현] 10820. 문자열 분석 (파이썬/Python) (0) | 2021.11.02 |
[백준][구현] 2747. 피보나치 수 (파이썬/Python) (0) | 2021.11.02 |