deno.land / x / masx200_leetcode_test@10.6.5 / design-circular-deque / index.ts

نووسراو ببینە
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
export default interface MyCircularDeque<T = any> { isEmpty: () => boolean; getFront: () => T | number; deleteFront: () => boolean; insertLast: (value: T) => boolean; insertFront: (value: T) => boolean; deleteLast: () => boolean; getRear: () => T | number; isFull: () => boolean; capacity: number;}
export default function MyCircularDeque<T = any>( capacity = Infinity,): MyCircularDeque<T> { if (capacity < 1) throw Error("k greater than or equal one"); const storage = new Map<bigint, T>(); let min = BigInt(0); let max = BigInt(0); const initial = 0n; function insertLast(value: T): boolean { if (isFull()) return false;
if (storage.size) { storage.set(max + 1n, value); max++; } else { storage.set(initial, value); max = initial; }
return true; }
function deleteFront(): boolean { if (isEmpty()) return false; else { storage.delete(min); min++; if (storage.size === 0) { min = initial; } return true; } }
function getFront(): T | number { if (isEmpty()) return -1; const r = storage.get(min); return r as T; }
function isEmpty(): boolean { return 0 === storage.size; } function insertFront(value: T): boolean { if (isFull()) return false; if (storage.size) { storage.set(min - 1n, value); min--; } else { storage.set(initial, value); min = initial; } return true; } function deleteLast(): boolean { if (isEmpty()) return false; else { storage.delete(max); max--; if (storage.size === 0) { max = initial; } return true; } } function getRear(): T | number { if (isEmpty()) return -1;
const r = storage.get(max);
return r as T; } function isFull(): boolean { return storage.size >= capacity; } return { isEmpty, getFront, deleteFront, insertLast, insertFront, deleteLast, getRear, isFull, capacity: capacity, };}
masx200_leetcode_test

Version Info

Tagged at
a year ago