일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- leetcode
- 맥북
- github
- ChatGPT
- Git
- 자연어처리
- AI 경진대회
- 우분투
- Baekjoon
- 코로나19
- 데이콘
- Kaggle
- 캐치카페
- SW Expert Academy
- gs25
- 파이썬
- 편스토랑 우승상품
- dacon
- 편스토랑
- programmers
- 프로그래머스 파이썬
- Real or Not? NLP with Disaster Tweets
- 금융문자분석경진대회
- Docker
- ubuntu
- 더현대서울 맛집
- hackerrank
- PYTHON
- 프로그래머스
- 백준
- Today
- Total
솜씨좋은장씨
[HackerRank] Stack and Queues : A Tale of Two Stacks 본문
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed.
A basic queue has the following operations:
- Enqueue: add a new element to the end of the queue.
- Dequeue: remove the element from the front of the queue and return it.
In this challenge, you must first implement a queue using two stacks. Then process queries, where each query is one of the following 3 types:
- 1 x: Enqueue element x into the end of the queue.
- 2: Dequeue the element at the front of the queue.
- 3: Print the element at the front of the queue.
For example, a series of queries might be as follows:
Query | Lifo | Fifo | Output |
1 35 | { 35 } | { 35 } | |
1 15 | { 35, 15 } | { 15, 35 } | |
3 | 35 | ||
2 | { 15 } | { 15 } | |
3 | 15 |
Function Description
Complete the put, pop, and peek methods in the editor below. They must perform the actions as described above.
Input Format
The first line contains a single integer, q, the number of queries.
Each of the next q lines contains a single query in the form described in the problem statement above. All queries start with an integer denoting the query type, but only query 1 is followed by an additional space-separated value, x, denoting the value to be enqueued.
Constraints
- 1 <= q <= 10^5
- 1 <= type <= 3
- 1 <= | x | <= 10^9
- It is guaranteed that a valid answer always exists for each query of types 2 and 3.
Output Format
For each query of type 3, return the value of the element at the front of the fifo queue on a new line.
Sample Input
10
1 42
2
1 14
3
1 28
3
1 60
1 78
2
2
Sample Output
14
14
Explanation
Query | Fifo | Lifo | Output |
1 42 | { 42 } | { 42 } | |
2 | { } | { } | |
1 14 | { 14 } | { 14 } | |
3 | 14 | ||
1 28 | { 14, 28 } | { 28, 14 } | |
3 | 14 | ||
1 60 | { 14, 28, 60 } | { 60, 28, 14 } | |
1 78 | { 14, 28, 60, 78 } | { 78, 60, 28, 14 } | |
2 | { 28, 60, 78 } | { 78, 60, 28 } | |
2 | { 60, 78 } | { 78, 60 } |
Solution
class MyQueue(object):
def __init__(self):
self.my_queue = []
def peek(self):
return self.my_queue[0]
def pop(self):
self.my_queue = self.my_queue[1:]
def put(self, value):
self.my_queue.append(value)
queue = MyQueue()
t = int(input())
for line in range(t):
values = map(int, input().split())
values = list(values)
if values[0] == 1:
queue.put(values[1])
elif values[0] == 2:
queue.pop()
else:
print(queue.peek())
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[HackerRank] Time Conversion (Python) (0) | 2020.04.05 |
---|---|
[HackerRank] Birthday Cake Candles (Python) (0) | 2020.04.05 |
[HackerRank] Min-Max Sum (Python) (0) | 2020.04.03 |
[HackerRank] Staircase (Python) (0) | 2020.04.02 |
[BaekJoon] 2133번 : 타일 채우기 (Python) (0) | 2020.04.01 |