Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- 코딩테스트
- Python
- 개발자취업
- 99클럽
- 코테 파이썬
- 코테
- til
- react
- 개발자 취업
- 코딩테스트준비
- 파이썬
- 파이썬 코테
- 항해99
- vue
- 티스토리챌린지
- Flutter
- flutter getx
- c++ 코테
- 플러터
- 안드로이드
- 알고리즘
- 오블완
- C++
- ML
- 코딩테스트 준비
- Laravel
- 백준
- 라라벨
- 뷰
- DP
Archives
- Today
- Total
잡다로그
[Python/코테] 백준 10845번 큐 본문
10845 큐
문제 및 조건 설명: https://www.acmicpc.net/problem/10845


import sys
n = int(sys.stdin.readline())
queue = []
for i in range(n):
command = sys.stdin.readline().split()
if (command[0] == "push"):
queue.append(int(command[1]))
elif (command[0] == "pop"):
if (len(queue) != 0):
print(queue.pop(0))
else:
print(-1)
elif (command[0] == "size"):
print(len(queue))
elif (command[0] == "empty"):
if (len(queue) != 0):
print(0)
else:
print(1)
elif (command[0] == "front"):
if (len(queue) != 0):
print(queue[0])
else:
print(-1)
elif (command[0] == "back"):
if (len(queue) != 0):
print(queue[-1])
else:
print(-1)
스택을 구현했던 것처럼, 배열을 사용할 수 있다.
또는 덱(deque) 라이브러리를 사용해서 구현하는 방법도 있다. 그런데 메모리나 시간이 더 오래 걸리는 것은 왜일까

from collections import deque
import sys
n = int(sys.stdin.readline())
queue = deque([])
for _ in range(n):
command = sys.stdin.readline().split()
if (command[0] == "push"):
queue.append(int(command[1]))
elif (command[0] == "pop"):
print(-1 if len(queue) == 0 else queue.popleft())
elif (command[0] == "size"):
print(len(queue))
elif (command[0] == "empty"):
print(1 if len(queue) == 0 else 0)
elif (command[0] == "front"):
print(-1 if len(queue) == 0 else queue[0])
elif (command[0] == "back"):
print(-1 if len(queue) == 0 else queue[-1])'Algorithm' 카테고리의 다른 글
| [Python/코테] 백준 10866 (0) | 2023.11.09 |
|---|---|
| [C++/코테] 큐(queue) (0) | 2023.11.09 |
| [C++/코테] 스택(Stack) 기초 (0) | 2023.11.08 |
| [Python/코테] 백준 10828 스택 (0) | 2023.11.08 |
| [Python/코테] 백준 2577번 숫자의 개수 (0) | 2023.11.08 |
Comments