๊ธฐ๋ก๋ฐฉ

BOJ_10845 : ํ ๋ณธ๋ฌธ

CodingTest/Python

BOJ_10845 : ํ

Soom_1n 2022. 8. 11. 15:27

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

10845๋ฒˆ: ํ

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

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ํ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import sys
from collections import deque

input = sys.stdin.readline
print = sys.stdout.write

N = int(input().strip())

q = deque()

for i in range(N):
    order = input().strip()

    if order[:4] == "push":
        q.append(order[5:])
    elif order == "pop":
        if len(q) == 0:
            print("-1\n")
        else:
            print(str(q.popleft()) + "\n")
    elif order == "size":
        print(str(len(q)) + "\n")
    elif order == "empty":
        print(str(int(len(q) == 0)) + "\n")
    elif order == "front":
        if len(q) == 0:
            print("-1\n")
        else:
            print(str(q[0]) + "\n")
    elif order == "back":
        if len(q) == 0:
            print("-1\n")
        else:
            print(str(q[-1]) + "\n")

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ฐํฌ์™€ ๋น ๋ฅธ ์ž…์ถœ๋ ฅ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ์ž…๋ ฅ๋œ ๋ช…๋ น์„ ์Šฌ๋ผ์ด์‹ฑํ•ด์„œ ํ™•์ธํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋น ๋ฅธ ์ž…์ถœ๋ ฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
  • ํ’€์ด๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์ง€๋งŒ ์˜คํƒ€๊ฐ€ ์žˆ์–ด์„œ ์—ฌ๋Ÿฌ๋ฒˆ ํ‹€๋ฆฌ๊ฒŒ ๋˜์—ˆ๋‹ค.
  • queue๋ชจ๋“ˆ์˜ Queue() ๋„ ์จ๋ณด๋ ค ํ–ˆ์ง€๋งŒ ์œ„์˜ front ๊ธฐ๋Šฅ์ธ peek ๋ช…๋ น์ด ์—†์–ด์„œ ์ด ๋ฌธ์ œ์™€๋Š” ๋งž์ง€ ์•Š์•˜๋‹ค.

728x90

'CodingTest > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1935 : ํ›„์œ„ ํ‘œ๊ธฐ์‹2  (0) 2022.08.14
BOJ_17608 : ๋ง‰๋Œ€๊ธฐ  (0) 2022.08.14
BOJ_10816 : ์ˆซ์ž ์นด๋“œ 2  (0) 2022.08.10
BOJ_10773 : ์ œ๋กœ  (0) 2022.08.09
BOJ_4949 : ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ  (0) 2022.08.08