URL
https://leetcode.com/problems/design-circular-deque/description/?envType=daily-question&envId=2024-09-28
Code
https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202409/design_circular_deque/main.fsx
type MyCircularDeque =
{ Q: int[]
Start: int
End: int
Capacity: int
Size: int }
static member Init(k: int) : MyCircularDeque =
{ Q = Array.zeroCreate k
Start = 0
End = k - 1
Capacity = k
Size = 0 }
static member InsertFront (v: int) (q: MyCircularDeque) : bool * MyCircularDeque =
if MyCircularDeque.IsFull q then
false, q
else
let start = (q.Start - 1 + q.Capacity) % q.Capacity
q.Q.[start] <- v
true,
{ q with
Q = q.Q
Start = start
Size = q.Size + 1 }
static member InsertLast (v: int) (q: MyCircularDeque) : bool * MyCircularDeque =
if MyCircularDeque.IsFull q then
false, q
else
let end' = (q.End + 1) % q.Capacity
q.Q.[end'] <- v
true,
{ q with
Q = q.Q
End = end'
Size = q.Size + 1 }
static member DeleteFront(q: MyCircularDeque) : bool * MyCircularDeque =
if MyCircularDeque.IsEmpty q then
false, q
else
let start = (q.Start + 1) % q.Capacity
true,
{ q with
Start = start
Size = q.Size - 1 }
static member DeleteLast(q: MyCircularDeque) : bool * MyCircularDeque =
if MyCircularDeque.IsEmpty q then
false, q
else
let end' = (q.End - 1 + q.Capacity) + q.Capacity
true, { q with End = end'; Size = q.Size - 1 }
static member GetFront(q: MyCircularDeque) : int =
if MyCircularDeque.IsEmpty q then -1 else q.Q.[q.Start]
static member GetRear(q: MyCircularDeque) : int =
if MyCircularDeque.IsEmpty q then -1 else q.Q.[q.End]
static member IsEmpty(q: MyCircularDeque) : bool = q.Size = 0
static member IsFull(q: MyCircularDeque) : bool = q.Size = q.Capacity