๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. ํ2 (18258) [python][Silver IV]

ttoance 2023. 2. 20. 19:56

๋ฌธ์ œ : 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

n = int(input())

queue = deque([])

for i in range(n):
    str =  sys.stdin.readline()
    strArray = str.split()
  
    command = strArray[0]
    if len(strArray) > 1:
        args = strArray[1]
        
        
    if (command == 'push') :
        queue.append(args)
    elif (command == 'pop') :
        if not queue:
            print(-1)
        else:
            print(queue.popleft())
    elif (command == 'size') :
        print(len(queue))
    elif (command == 'empty') :
        if not queue:
            print(1)
        else:
            print(0)
    elif (command == 'front') :
        if not queue:
            print(-1)
        else:
            print(queue[0])
    elif (command == 'back') :
        if not queue:
            print(-1)
        else:
            print(queue[-1])

 


https://zetawiki.com/wiki/Python_deque_%ED%81%90

 

Python deque ํ - ์ œํƒ€์œ„ํ‚ค

๋‹ค์Œ ๋ฌธ์ž์—ด ํฌํ•จ...

zetawiki.com

 

๋ฐ˜์‘ํ˜•