[ Python Coding ] 백준 18258. 큐2

Hyeon_D's Log ღ·2021년 11월 9일
0

BAEKJOON

목록 보기
7/8
post-thumbnail

문제

입력 및 출력

실행 코드

from collections import deque
import sys

input = sys.stdin.readline # 시간초과 문제 해결
q = deque() # 큐 구현하려면 라이브러리 사용해야함

n = int(input())


for i in range(n):
    check = input().split()
    
    if check[0] == 'push':
        q.append(check[1])
        
    elif check[0] == 'pop':
        if len(q) == 0:
            print(-1)
        else:
            print(q.popleft())
            
    elif check[0] == 'size':
        print(len(q))
            
    elif check[0] == 'empty':
        if len(q) == 0:
            print(1)
        else:
            print(0)
            
    elif check[0] == 'front':
        if len(q) == 0:
            print(-1)
        else:
            print(q[0])
            
    elif check[0] == 'back':
        if len(q) == 0:
            print(-1)
        else:
            print(q[-1])

풀이

  • for문을 통해 입력된 문자에 해당하는 명령을 실행한다.
  • 큐를 구현하려면 deque 라이브러리를 사용해야한다.
  • input 대신 sys.stdin.readlin 을 작성해야 시간초과가 안난다.

[문제 링크]
https://www.acmicpc.net/problem/18258

profile
Data Scientist 🧐

0개의 댓글

관련 채용 정보