사용자 도구

사이트 도구


ps:problems:boj:10845

ps
링크acmicpc.net/…
출처BOJ
문제 번호10845
문제명
레벨실버 4
분류

시간복잡도O(n)
인풋사이즈n<=10,000
사용한 언어Python
제출기록31780KB / 96ms
최고기록60ms
해결날짜2021/10/07

풀이

  • '큐'를 이용해서 기본적인 연산들을 수행할수 있는지 물어보는 기본 문제. 유사품으로 스택, 이 있다.
  • 라이브러리에 있는 큐를 이용해서 주어진 연산들을 처리해주면 끝이다
  • 주어진 연산들은 큐에서 모두 O(1)에 처리되므로 총 시간복잡도는 O(n).

코드

"""Solution code for "BOJ 10845. 큐".

- Problem link: https://www.acmicpc.net/problem/10845
- Solution link: http://www.teferi.net/ps/problems/boj/10845

Tags: [queue]
"""

import collections
import sys


def main():
    N = int(sys.stdin.readline())
    queue = collections.deque()
    for _ in range(N):
        oper = sys.stdin.readline().split()
        op = oper[0]
        if op == 'push':
            queue.append(oper[1])
        elif op == 'pop':
            print(queue.popleft() if queue else '-1')
        elif op == 'size':
            print(len(queue))
        elif op == 'empty':
            print('0' if queue else '1')
        elif op == 'front':
            print(queue[0] if queue else '-1')
        elif op == 'back':
            print(queue[-1] if queue else '-1')


if __name__ == '__main__':
    main()

토론

댓글을 입력하세요:
G P P T W
 
ps/problems/boj/10845.txt · 마지막으로 수정됨: 2021/10/15 16:14 저자 teferi