| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 파이썬
- mvc
- 소수찾기
- c#
- 그리디 알고리즘
- 합 구하기
- pypy3
- C#강의
- 1일1솔
- deque
- 프로그래머스
- popleft
- DP
- 백준
- 브루투포스
- python3
- 우선순위큐
- spring
- 인프런
- 소수판별
- LCM
- 연관관계
- 완전탐색
- unity
- JPA
- Python
- Java
- appendleft
- BFS
- 누적합
Archives
- Today
- Total
jae_coding
(백준 알고리즘 문제풀이) 18258번 큐 2 본문
반응형
문제
코드
import sys
from collections import deque
input = sys.stdin.readline
def push(lst, num):
lst.append(num)
def pop(lst):
if len(lst) != 0:
print(lst[0])
lst.popleft()
else:
print(-1)
def size(lst):
print(len(lst))
def empty(lst):
if len(lst) != 0:
print(0)
else:
print(1)
def front(lst):
if len(lst) != 0:
print(lst[0])
else:
print(-1)
def back(lst):
if len(lst) != 0:
print(lst[-1])
else:
print(-1)
N = int(input())
# list를 사용하면 O(n)으로 시간초과가 나기때문에 deque를 이용해준다.
queue = deque()
for _ in range(N):
s = input().split()
command = s[0]
if command == "push":
num = s[1]
push(queue, int(num))
elif command == "front":
front(queue)
elif command == "back":
back(queue)
elif command == "size":
size(queue)
elif command == "empty":
empty(queue)
elif command == "pop":
pop(queue)반응형
'알고리즘 문제 > 자료구조 문제' 카테고리의 다른 글
| (백준 자료구조 문제풀이) 1874번 스택 수열 (0) | 2022.07.19 |
|---|---|
| (백준 자료구조 문제풀이) 10866번 덱 (0) | 2022.07.18 |
| (백준 자료구조 문제풀이) 1158번 요세푸스 문제 (0) | 2022.07.18 |
| (백준 자료구조 문제풀이) 9012번 괄호 (0) | 2022.07.18 |
| (백준 자료구조 문제풀이) 10828번 스택 (0) | 2022.07.18 |
Comments