๊ธฐ๋ก๋ฐฉ

BOJ_18258 : ํ 2 ๋ณธ๋ฌธ

CodingTest/Python

BOJ_18258 : ํ 2

Soom_1n 2022. 8. 15. 19:28

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

 

18258๋ฒˆ: ํ 2

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

www.acmicpc.net



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

  • front, back ์„ ์ง€์›ํ•˜๋Š” queue๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

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

import sys
from collections import deque
input = sys.stdin.readline
print = sys.stdout.write

que = deque()

for i in range(int(input())):
    order = input().rstrip()

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

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

  • ์‹œ๊ฐ„์ œํ•œ์ด ๋น ๋“ฏํ•ด์„œ ๋น ๋ฅธ ์ž…์ถœ๋ ฅ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
    • ์ž…๋ ฅ์—์„œ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž "\n"๊ฐ€ ์ž…๋ ฅ๋˜์ง€ ์•Š๋„๋ก strip() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
    • ์ถœ๋ ฅ์—์„œ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ง์ ‘ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
    • ๋น ๋ฅธ ์ถœ๋ ฅ์€ ๋ฌธ์ž์—ด๋งŒ ์ง€์›ํ•˜๋ฏ€๋กœ ์ˆซ์ž๋Š” str()๋กœ ๋ณ€ํ™˜ํ–ˆ๋‹ค.
  • ํ๋Š” ๋ฐํฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ํ’€๊ณ ๋ณด๋‹ˆ ํ 1๋ฌธ์ œ์™€ ํ’€์ด๊ฐ€ ๋˜‘๊ฐ™๋‹ค. ์‹œ๊ฐ„ ์ œํ•œ๋งŒ ๋” ๋น ๋“ฏํ•ด์ง„ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋ฅผ ๋„ฃ์ง€ ์•Š์€ ๊ณณ์ด ์žˆ์–ด์„œ 1์ฐจ ํ‹€๋ ธ๊ณ , popleft๋ฅผ ํ•ด์•ผํ•˜๋Š”๋ฐ pop์„ ์‚ฌ์šฉํ•ด์„œ 2์ฐจ ํ‹€๋ ธ๋‹ค.
    ์˜ˆ์ œ ์ถœ๋ ฅ๋งŒ ์ž˜ ํ™•์ธํ–ˆ์œผ๋ฉด ๊ณ ์น  ์ˆ˜ ์žˆ์—ˆ์„ํ…๋ฐ ์ข€ ์•ˆ์ผํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

728x90

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

BOJ_2161 : ์นด๋“œ1  (0) 2022.08.16
BOJ_12873 : ๊ธฐ๋…ํ’ˆ  (0) 2022.08.15
BOJ_1935 : ํ›„์œ„ ํ‘œ๊ธฐ์‹2  (0) 2022.08.14
BOJ_17608 : ๋ง‰๋Œ€๊ธฐ  (0) 2022.08.14
BOJ_10845 : ํ  (0) 2022.08.11