<LeetCode 문제>
https://leetcode.com/problems/design-circular-queue/
Design Circular Queue - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
<풀이>
class MyCircularQueue:
def __init__(self, k: int):
self.q = [None]*k #배열로 원형큐 정의
self.maxlen = k #최대길이 저장
self.p1 = 0 #front 포인터
self.p2 = 0 #rear 포인터
#rear 포인터 이동
def enQueue(self, value: int) -> bool:
if self.q[self.p2] is None:
self.q[self.p2]=value
self.p2 = (self.p2 + 1) % self.maxlen #rear 포인터 갱신
return True
else: #rear 포인터가 None이면 더 추가 불가
return False
#front 포인터 이동
def deQueue(self) -> bool:
if self.q[self.p1] is None:
return False
else:
self.q[self.p1] = None
self.p1 = (self.p1+1)%self.maxlen
return True
def Front(self) -> int:
return -1 if self.q[self.p1] is None else self.q[self.p1]
def Rear(self) -> int:
return -1 if self.q[self.p2-1] is None else self.q[self.p2-1]
def isEmpty(self) -> bool:
return self.p1 == self.p2 and self.q[self.p1] is None
def isFull(self) -> bool:
return self.p1 == self.p2 and self.q[self.p1] is not None
# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()
<학습내용>
1.
<학습이 필요한 내용>
1. 큐, 스택, 덱에 사용되는 연산자들 정리
'Algorithm > 파이썬 알고리즘 인터뷰 스터디' 카테고리의 다른 글
[데크, 우선순위 큐] 27 k개 정렬 리스트 병합(Merge k Sorted Lists) (0) | 2021.08.21 |
---|---|
[데크, 우선순위 큐] 26 원형 데크 디자인(Design Circular Deque) (0) | 2021.08.21 |
[스택, 큐] 24 스택을 이용한 큐 구현(Implement Queue using Stacks) - 작성중 (0) | 2021.08.16 |
[스택, 큐] 23 큐를 이용한 스택 구현(Implement Stack using Queues) - 작성중 (0) | 2021.08.16 |
[스택, 큐] 22 일일 온도(Daily Temperatures) - 작성중 (0) | 2021.08.16 |