-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.js
109 lines (106 loc) · 3.79 KB
/
solution.js
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
/**
* @param {number[][]} orders
* @return {number}
*/
var getNumberOfBacklogOrders = function (orders) {
// 小顶堆
const sellHeap = [];
// 大顶堆
const buyHeap = [];
for (let i = 0; i < orders.length; i++) {
let [pricei, amounti, orderTypei, ] = orders[i];
if (orderTypei === 0) {
// buy
while (amounti > 0 && sellHeap.length && sellHeap[0][0] <= pricei) {
if (sellHeap[0][1] > amounti) {
sellHeap[0][1] -= amounti;
amounti = 0;
} else {
amounti -= sellHeap[0][1];
sellHeap[0] = sellHeap[sellHeap.length - 1];
sellHeap.pop();
let index = 0;
while (2 * index + 1 < sellHeap.length) {
let child = 2 * index + 1;
if (child + 1 < sellHeap.length && sellHeap[child + 1][0] < sellHeap[child][0]) {
child++;
}
if (sellHeap[child][0] < sellHeap[index][0]) {
swap(sellHeap, index, child);
index = child;
} else {
break;
}
}
}
}
if (amounti > 0) {
const node = [pricei, amounti, ];
buyHeap.push(node);
let index = buyHeap.length - 1;
while (index > 0) {
const parent = ((index + 1) >> 1) - 1;
if (buyHeap[parent][0] < buyHeap[index][0]) {
swap(buyHeap, index, parent);
index = parent;
} else {
break;
}
}
}
} else {
// sell
while (amounti > 0 && buyHeap.length && buyHeap[0][0] >= pricei) {
if (buyHeap[0][1] > amounti) {
buyHeap[0][1] -= amounti;
amounti = 0;
} else {
amounti -= buyHeap[0][1];
buyHeap[0] = buyHeap[buyHeap.length - 1];
buyHeap.pop();
let index = 0;
while (2 * index + 1 < buyHeap.length) {
let child = 2 * index + 1;
if (child + 1 < buyHeap.length && buyHeap[child + 1][0] > buyHeap[child][0]) {
child++;
}
if (buyHeap[index][0] < buyHeap[child][0]) {
swap(buyHeap, index, child);
index = child;
} else {
break;
}
}
}
}
if (amounti) {
const node = [pricei, amounti, ];
sellHeap.push(node);
let index = sellHeap.length - 1;
while (index > 0) {
const parent = ((index + 1) >> 1) - 1;
if (sellHeap[parent][0] > sellHeap[index][0]) {
swap(sellHeap, index, parent);
index = parent;
} else {
break;
}
}
}
}
}
let result = 0;
const mod = 10 ** 9 + 7;
for (let i = 0; i < buyHeap.length; i++) {
result = (result + buyHeap[i][1]) % mod;
}
for (let i = 0; i < sellHeap.length; i++) {
result = (result + sellHeap[i][1]) % mod;
}
return result;
};
function swap (array, i, j) {
const tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}