-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path2163-minimum-difference-in-sums-after-removal-of-elements.js
158 lines (149 loc) · 3.76 KB
/
2163-minimum-difference-in-sums-after-removal-of-elements.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
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
/**
* @param {number[]} nums
* @return {number}
*/
const minimumDifference = function(nums) {
const n = nums.length, len = n / 3
const maxCompare = (p, c) => { return p === c ? 0 : (p > c ? -1 : 1)}
const minCompare = (p, c) => { return p === c ? 0 : (p < c ? -1 : 1)}
const maxHeap = new PriorityQueue({compare: maxCompare})
const minHeap = new PriorityQueue({compare: minCompare})
const pre = Array(n).fill(Infinity), suffix = Array(n).fill(-Infinity)
for(let i = 0, sum = 0; i < 2 * len; i++) {
const cur = nums[i]
maxHeap.enqueue(cur)
sum += cur
if(maxHeap.size() > len) {
const tmp = maxHeap.dequeue()
sum -= tmp
}
if(maxHeap.size() === len) {
pre[i] = sum
}
}
for(let i = n - 1, sum = 0; i >= len; i--) {
const cur = nums[i]
minHeap.enqueue(cur)
sum += cur
if(minHeap.size() > len) {
const tmp = minHeap.dequeue()
sum -= tmp
}
if(minHeap.size() === len) {
suffix[i] = sum
}
}
// console.log(pre, suffix)
let res = Infinity
for(let i = len - 1; i < n - len; i++) {
res = Math.min(res, pre[i] - suffix[i + 1])
}
return res
};
// another
/**
* @param {number[]} nums
* @return {number}
*/
const minimumDifference = function(nums) {
const n = nums.length, len = n / 3
const maxHeap = new PriorityQueue((a, b) => a > b)
const minHeap = new PriorityQueue((a, b) => a < b)
const pre = Array(n).fill(Infinity), suffix = Array(n).fill(-Infinity)
for(let i = 0, sum = 0; i < 2 * len; i++) {
const cur = nums[i]
maxHeap.push(cur)
sum += cur
if(maxHeap.size() > len) {
const tmp = maxHeap.pop()
sum -= tmp
}
if(maxHeap.size() === len) {
pre[i] = sum
}
}
for(let i = n - 1, sum = 0; i >= len; i--) {
const cur = nums[i]
minHeap.push(cur)
sum += cur
if(minHeap.size() > len) {
const tmp = minHeap.pop()
sum -= tmp
}
if(minHeap.size() === len) {
suffix[i] = sum
}
}
// console.log(pre, suffix)
let res = Infinity
for(let i = len - 1; i < n - len; i++) {
res = Math.min(res, pre[i] - suffix[i + 1])
}
return res
};
class PriorityQueue {
constructor(comparator = (a, b) => a > b) {
this.heap = []
this.top = 0
this.comparator = comparator
}
size() {
return this.heap.length
}
isEmpty() {
return this.size() === 0
}
peek() {
return this.heap[this.top]
}
push(...values) {
values.forEach((value) => {
this.heap.push(value)
this.siftUp()
})
return this.size()
}
pop() {
const poppedValue = this.peek()
const bottom = this.size() - 1
if (bottom > this.top) {
this.swap(this.top, bottom)
}
this.heap.pop()
this.siftDown()
return poppedValue
}
replace(value) {
const replacedValue = this.peek()
this.heap[this.top] = value
this.siftDown()
return replacedValue
}
parent = (i) => ((i + 1) >>> 1) - 1
left = (i) => (i << 1) + 1
right = (i) => (i + 1) << 1
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
siftUp = () => {
let node = this.size() - 1
while (node > this.top && this.greater(node, this.parent(node))) {
this.swap(node, this.parent(node))
node = this.parent(node)
}
}
siftDown = () => {
let node = this.top
while (
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
(this.right(node) < this.size() && this.greater(this.right(node), node))
) {
let maxChild =
this.right(node) < this.size() &&
this.greater(this.right(node), this.left(node))
? this.right(node)
: this.left(node)
this.swap(node, maxChild)
node = maxChild
}
}
}