deno.land / x / masx200_leetcode_test@10.6.5 / stack-of-plates-lcci / 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
const capacity = Symbol();const stacks = Symbol();class StackOfPlates { [stacks]: number[][] = []; [capacity]: number; constructor(cap: number) { this[capacity] = cap; }
push(val: number): void { if (this[capacity] <= 0) return;
if ( this[stacks].length === 0 || this[stacks][this[stacks].length - 1].length >= this[capacity] ) { this[stacks].push([val]); } else { this[stacks][this[stacks].length - 1].push(val); } }
pop(): number { if (this[capacity] <= 0) return -1; if (this[stacks].length === 0) return -1;
const lastStack = this[stacks][this[stacks].length - 1]; const val = lastStack.pop(); if (lastStack.length === 0) { this[stacks].pop(); } return val ?? -1; }
popAt(index: number): number { if (this[capacity] <= 0) return -1; if (this[stacks].length === 0) return -1; const currentStack = this[stacks][index]; if (!currentStack) { return -1; } const val = currentStack.pop(); if (currentStack.length === 0) { this[stacks].splice(index, 1); } return val ?? -1; }}export default StackOfPlates;
masx200_leetcode_test

Version Info

Tagged at
a year ago