๊ธฐ๋ก๋ฐฉ

BOJ_10866 : ๋ฑ ๋ณธ๋ฌธ

CodingTest/Python

BOJ_10866 : ๋ฑ

Soom_1n 2022. 8. 17. 21:51

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

 

10866๋ฒˆ: ๋ฑ

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

www.acmicpc.net



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

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

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

from collections import deque
import sys

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

deq = deque()

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

    if order[:10] == "push_front":
        deq.appendleft(order[11:])
    elif order[:9] == "push_back":
        deq.append(order[10:])
    elif order[:9] == "pop_front":
        if len(deq):
            print(deq.popleft() + "\n")
        else:
            print("-1\n")
    elif order[:8] == "pop_back":
        if len(deq):
            print(deq.pop() + "\n")
        else:
            print("-1\n")
    elif order[:4] == "size":
        print(str(len(deq)) + "\n")
    elif order[:5] == "empty":
        print(str(int(len(deq) == 0)) + "\n")
    elif order[:5] == "front":
        if len(deq):
            print(deq[0] + "\n")
        else:
            print("-1\n")
    elif order[:4] == "back":
        if len(deq):
            print(deq[-1] + "\n")
        else:
            print("-1\n")

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

  • collenctions ๋ชจ๋“ˆ์˜ deque ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ๋น ๋ฅธ ์ž…์ถœ๋ ฅ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ํ•œ ์ค„์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. (๋น ๋ฅธ ์ž…์ถœ๋ ฅ ํ˜•ํƒœ์— ์œ ์˜ํ•œ๋‹ค)

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ฐ„0๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค. ๋ฐํฌ์˜ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์„ ๋‘๋ฃจ ์จ๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์•˜๋‹ค.

728x90

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

BOJ_2805 : ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ  (0) 2022.08.20
BOJ_2108 : ํ†ต๊ณ„ํ•™  (0) 2022.08.18
BOJ_15828 : Router  (0) 2022.08.16
BOJ_2161 : ์นด๋“œ1  (0) 2022.08.16
BOJ_12873 : ๊ธฐ๋…ํ’ˆ  (0) 2022.08.15