Coding/백준

[백준] 10845 - 큐 - (python 파이썬)

Coding Kitsune 2022. 2. 3. 11:12

 

문제링크 : 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)

답(이미지)

 

후기 : 웬일로 고민없이 잘 해결되어서 후기라 할것도 없다...>.~