Tags
- greedy
- stack
- ์๋ฃ๊ตฌ์กฐ
- ๋๋น ์ฐ์ ํ์
- Study
- Brute Force Algorithm
- ์๋ฎฌ๋ ์ด์
- ์ ์๋ก
- ๊ทธ๋ํ ์ด๋ก
- DP
- ๊น์ด ์ฐ์ ํ์
- dfs
- PGM
- SpringBoot
- BOJ
- ๊ตฌํ
- ๋ฐฑํธ๋ํน
- Java
- ์ ๋ ฌ
- ๊ทธ๋ํ ํ์
- BFS
- LV2
- CodingTest
- Dynamic Programming
- Python
- queue
- ๊ต์ฌ
- ์ํ
- ๋ฌธ์์ด
- sort
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_10845 : ํ ๋ณธ๋ฌธ
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 |