728x90
๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
641. design_circular_deque | 57.2% | Medium |
Design Circular Deque - LeetCode
Can you solve this real interview question? Design Circular Deque - Design your implementation of the circular double-ended queue (deque). Implement the MyCircularDeque class: * MyCircularDeque(int k) Initializes the deque with a maximum size of k. * boole
leetcode.com
๋ฌธ์ ์์ฝ
๋ค์ ์ฐ์ฐ์ ์ ๊ณตํ๋ ์ํ ๋ฐํฌ๋ฅผ ๋ง๋ค์ด๋ผ
- def __init__(self, k: int): k ํฌ๊ธฐ์ ์ํ๋ฐ๋ฅผ ์ ์ธ
- def insertFront(self, value: int) -> bool: value ๊ฐ์ head ์ผ์ชฝ์ ์ฝ์ ํ๋ ํจ์
- def insertLast(self, value: int) -> bool: value ๊ฐ์ tail ์ค๋ฅธ์ ์ฝ์ ํ๋ ํจ์
- def deleteFront(self) -> bool: ์ํ๋ฐํฌ์ head ์ธ๋ฑ์ค์ ๊ฐ์ ์ญ์ ํ๋ ํจ์
- def deleteLast(self) -> bool: ์ํ๋ฐํฌ์ tail ์ธ๋ฑ์ค์ ๊ฐ์ ์ญ์ ํ๋ ํจ์
- def Front(self) -> int: ์ํ๋ฐํฌ์ head ์ธ๋ฑ์ค์ ๊ฐ์ ๋ฐํํ๋ ํจ์
- def Rear(self) -> int: ์ํ๋ฐํฌ์ tail ์ธ๋ฑ์ค์ ๊ฐ์ ๋ฐํํ๋ ํจ์
- def isEmpty(self) -> bool: ์ํ๋ฐํฌ๊ฐ ๋น์๋์ง ํ๋จํ๋ ํจ์
- def isFull(self) -> bool: ์ํ๋ฐํฌ์ ์ฌ๋ถ์ ์๋ฆฌ๊ฐ ์๋์ง ํ๋จํ๋ ํจ์
ํ์ด 1
์ํ ๋ํ๋ ๋ฆฌ์คํธ์, head, tail ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
๊ตฌํ ์์๋ ๊ตฌํํ๊ธฐ ์ฉ์ดํ ๊ฒ๋ถํฐ ํ๋ค
๋ชจ๋ ํจ์๋ฅผ ๊ตฌํํ ๋,
- ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ
- head, tail ์ธ๋ฑ์ค๊ฐ ๋ฆฌ์คํธ์ ์ ์ผ ์ฒซ์๋ฆฌ(0)์ ์ ์ผ ๋์๋ฆฌ(len(์ํํ)-1)์ ์๋ ๊ฒฝ์ฐ๋ฅผ ๋๋์ด ์๊ฐํ์(ํด๋น ์ธ๋ฑ์ค์ ๋ค์ ์ธ๋ฑ์ค๋ ์ ์ผ ๋์๋ฆฌ์ ์ ์ผ ์ฒซ์๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ด๋ค)
__init__
front, rear
isEmpty, isFull
insertFront, insertLast, deleteFront, deleteLast
๊ตฌํ (96ms)
class MyCircularDeque:
def __init__(self, k: int):
self.head, self.tail=0, 0
self.q=[None]*k
def insertFront(self, value: int) -> bool:
if self.isFull():
return False
if self.isEmpty()==False:
if self.head==0:
self.head=len(self.q)-1
else:
self.head-=1
self.q[self.head]=value
return True
def insertLast(self, value: int) -> bool:
if self.isFull():
return False
if self.isEmpty()==False:
if self.tail==len(self.q)-1:
self.tail=0
else:
self.tail+=1
self.q[self.tail]=value
return True
def deleteFront(self) -> bool:
if self.isEmpty():
return False
self.q[self.head]=None
if self.head!=self.tail:
if self.head==len(self.q)-1:
self.head=0
else:
self.head+=1
return True
def deleteLast(self) -> bool:
if self.isEmpty():
return False
self.q[self.tail]=None
if self.head!=self.tail:
if self.tail==0:
self.tail=len(self.q)-1
else:
self.tail-=1
return True
def getFront(self) -> int:
if self.isEmpty():
return -1
return self.q[self.head]
def getRear(self) -> int:
if self.isEmpty():
return -1
return self.q[self.tail]
def isEmpty(self) -> bool:
return self.q[self.head]==None
def isFull(self) -> bool:
if self.tail==len(self.q)-1:
return self.head==0 and self.tail!=None
else: #tail<head ์ธ ๊ฒฝ์ฐ
return self.tail==(self.head-1)
728x90
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น (Lv.3) 2024/4/1 (1) | 2024.04.01 |
---|---|
[Programmers] ์๋ฌผ์ ์ ์ด์ (Lv.3) 2024/3/31 (0) | 2024.03.31 |
[LeetCode] 622. design_circular_queue (Medium) 2023/5/10 (0) | 2023.05.10 |
[LeetCode] 232. implement_queue_using_stacks (Easy) 2023/5/10 (0) | 2023.05.10 |
[LeetCode] 739. daily_temperatures (Medium) 2023/5/9 (0) | 2023.05.09 |