-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
31 lines (28 loc) · 860 Bytes
/
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
export default class CustomStack {
#stack = Array<[number, number]>();
constructor(public capacity: number) {}
push(x: number): void {
if (this.#stack.length >= this.capacity) {
return;
}
this.#stack.push([x, 0]);
}
pop(): number {
if (this.#stack.length === 0) return -1;
const last = this.#stack[this.#stack.length - 1];
this.#stack.pop();
if (this.#stack.length) {
this.increment(this.#stack.length, last[1]);
}
return last[0] + last[1];
}
increment(k: number, val: number): void {
if (this.#stack.length === 0) return;
// console.log(this,k,val)
if (this.#stack.length < k) {
k = this.#stack.length;
}
// console.log(this,k,val)
this.#stack[k - 1][1] += val;
}
}