-
Notifications
You must be signed in to change notification settings - Fork 1
/
dinner-plate-stacks.ts
167 lines (156 loc) · 4.92 KB
/
dinner-plate-stacks.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 餐盘栈
// https://leetcode.cn/problems/dinner-plate-stacks
// INLINE ../../images/stack/dinner-plate-stacks.jpeg
// 参考:https://leetcode.cn/problems/dinner-plate-stacks/solution/typescriptshi-xian-wei-hu-shuang-dui-don-0s0t/
class HeapSort {
private arr: number[]
private compare: (a: number, b: number) => boolean
public constructor (compare: (a: number, b: number) => boolean) {
this.arr = []
this.compare = compare
}
private calcHeight (index: number): number {
return Math.floor(Math.log(index + 1) / Math.log(2)) + 1
}
private calcFatherIndex (index: number): number {
if (index === 0) {
return -1
}
const h = this.calcHeight(index)
return Math.floor((index - Math.pow(2, h - 1) + 1) / 2) + Math.pow(2, h - 2) - 1
}
private calcChildIndex (index: number): number[] {
return [2 * index + 1, 2 * index + 2]
}
private displayToLeaf (index: number) {
const { arr } = this
let minIndex = index
const children = this.calcChildIndex(index)
if (children[0] <= arr.length && this.compare(arr[minIndex], arr[children[0]])) {
/* istanbul ignore next */
minIndex = children[0]
}
if (children[1] <= arr.length && this.compare(arr[minIndex], arr[children[1]])) {
/* istanbul ignore next */
minIndex = children[1]
}
if (minIndex === index) return;
/* istanbul ignore next */
[this.arr[index], this.arr[minIndex]] = [arr[minIndex], arr[index]]
/* istanbul ignore next */
this.displayToLeaf(minIndex)
}
public deleteByValue (value: number) {
const { arr } = this
const index = arr.indexOf(value)
const len = arr.length
if (index !== -1) {
[this.arr[index], this.arr[len - 1]] = [arr[len - 1], arr[index]]
this.arr.pop()
this.displayToLeaf(index)
}
}
public push (value: number) {
const { arr } = this
const len = arr.length
this.arr.push(value)
let index = len
let fatherIndex = this.calcFatherIndex(index)
while (fatherIndex !== -1) {
const fatherValue = this.arr[fatherIndex]
// console.log(value, len, fatherValue, fatherIndex, this.compare(fatherValue, value))
if (this.compare(fatherValue, value)) {
[this.arr[fatherIndex], this.arr[index]] = [value, fatherValue]
}
index = fatherIndex
fatherIndex = this.calcFatherIndex(fatherIndex)
}
}
// return head
// public pop (): number {
// const { arr } = this
// const len = arr.length;
// [this.arr[len - 1], this.arr[0]] = [this.arr[0], this.arr[len - 1]]
// const res = this.arr.pop() ?? -1
// this.displayToLeaf(0)
// return res
// }
public print () {
return this.arr[0]
}
}
export class DinnerPlates {
private capacity: number
private stackNum: number
private stacks: number[][]
private notEmptyBitMap: HeapSort
private notFullBitMap: HeapSort
public constructor (capacity: number) {
this.capacity = capacity
this.stackNum = 0
this.stacks = []
this.notEmptyBitMap = new HeapSort((a, b) => a <= b)
this.notFullBitMap = new HeapSort((a, b) => a >= b)
}
push (val: number): void {
const { notFullBitMap, capacity } = this
const notFullIndex = notFullBitMap.print() ?? -1
if (notFullIndex !== -1) {
this.stacks[notFullIndex].push(val)
if (this.stacks[notFullIndex].length === capacity) {
this.notFullBitMap.deleteByValue(notFullIndex)
}
if (this.stacks[notFullIndex].length === 1) {
/* istanbul ignore next */
this.notEmptyBitMap.push(notFullIndex)
}
return
}
this.stacks.push([val])
this.stackNum++
if (capacity !== 1) this.notFullBitMap.push(this.stackNum - 1)
this.notEmptyBitMap.push(this.stackNum - 1)
return
}
pop (): number {
const { notEmptyBitMap, capacity } = this
let res = -1
const notEmptyIndex = notEmptyBitMap.print() ?? -1
if (notEmptyIndex !== -1) {
res = this.stacks[notEmptyIndex].pop() as number
if (this.stacks[notEmptyIndex].length === capacity - 1) {
this.notFullBitMap.push(notEmptyIndex)
}
if (this.stacks[notEmptyIndex].length === 0) {
this.notEmptyBitMap.deleteByValue(notEmptyIndex)
}
}
return res
}
popAtStack (index: number): number {
const { stackNum, capacity } = this
let res = -1
if (index >= stackNum || index < 0) {
/* istanbul ignore next */
return -1
}
if (this.stacks[index].length > 0) {
res = this.stacks[index].pop() as number
if (this.stacks[index].length === capacity - 1) {
this.notFullBitMap.push(index)
}
if (this.stacks[index].length === 0) {
/* istanbul ignore next */
this.notEmptyBitMap.deleteByValue(index)
}
}
return res
}
}
/**
* Your DinnerPlates object will be instantiated and called as such:
* var obj = new DinnerPlates(capacity)
* obj.push(val)
* var param_2 = obj.pop()
* var param_3 = obj.popAtStack(index)
*/