๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/BAEKJOON

[baekjoon]python #18258 ํ2

by DevIseo 2022. 11. 17.

[baekjoon]python #18258 ํ2

https://www.acmicpc.net/problem/18258

 

18258๋ฒˆ: ํ 2

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 2,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
queue = deque()
for i in range(N):
    temp = list(map(str,input().split()))

    if temp[0] == 'push':
        queue.append(int(temp[1]))

    elif temp[0] == 'front':
        if len(queue) != 0:
            temp = queue.popleft()
            queue.appendleft(temp)
            print(temp)
        else:
            print(-1)

    elif temp[0] == 'back':
        if len(queue) !=0:
            temp = queue.pop()
            queue.append(temp)
            print(temp)
        else:
            print(-1)

    elif temp[0] == 'size':
        print(len(queue))

    elif temp[0] == 'empty':
        if len(queue)==0:
            print(1)
        else:
            print(0)

    else:
        if len(queue)!=0:
            print(queue.popleft())
        else:
            print(-1)

๋Œ“๊ธ€