문제링크 : https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
풀이과정 :
이전문제였던 스택과 거의 동일한 방식으로, 결국 큐를 구현할 수 있느냐 묻는문제다.
자료구조 수업때 배웠던 대로 큐 클래스를 구현해서 풀까도 생각했지만, 그냥 남들처럼
포문안에 다 때려넣었다... ;ㅅ;... 다행히 원큐에 통과!
삽입하는건 스택과 동일한 방식이기 때문에 다른게 없을것이고, 디큐하는 부분이 포인트,
결국 큐 입장의 가장 앞을 가르키는 front_index를 추적하면서 리스트를 슬라이싱 하여,
어디까지가 큐냐를 생각하는것이 포인트다.
import sys
lst = []
front_index = 0
testcase = int(input())
for _ in range(testcase):
command = list(sys.stdin.readline().split())
if command[0] == "push": lst.append(int(command[1]))
elif command[0] == "pop":
if len(lst[front_index:]) != 0:
print(int(lst[front_index]))
front_index += 1
else: print(-1)
elif command[0] == "size":
print(len(lst[front_index:]))
elif command[0] == "empty":
if len(lst[front_index:]) != 0: print(0)
else: print(1)
elif command[0] == "front":
if len(lst[front_index:]) != 0: print(int(lst[front_index]))
else: print(-1)
elif command[0] == "back":
if len(lst[front_index:]) != 0: print(int(lst[-1]))
else: print(-1)
후기 : 웬일로 고민없이 잘 해결되어서 후기라 할것도 없다...>.~
'Coding > 백준' 카테고리의 다른 글
[백준] 10866 - 덱 - (python 파이썬) (0) | 2022.02.04 |
---|---|
[백준] 1158 - 요세푸스 문제 - (python 파이썬) (0) | 2022.02.03 |
[백준] 1406 - 에디터 - (python 파이썬) (0) | 2022.02.02 |
[백준] 10828 - 스택 수열 - (python 파이썬) (0) | 2022.02.01 |
[백준] 10828 - 스택 - (python 파이썬) (0) | 2022.01.31 |