Skip to content

Commit 2709eab

Browse files
authored
Create 2163-minimum-difference-in-sums-after-removal-of-elements.js
1 parent 0b75413 commit 2709eab

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const minimumDifference = function(nums) {
6+
const n = nums.length, len = n / 3
7+
const maxHeap = new PriorityQueue((a, b) => a > b)
8+
const minHeap = new PriorityQueue((a, b) => a < b)
9+
const pre = Array(n).fill(Infinity), suffix = Array(n).fill(-Infinity)
10+
for(let i = 0, sum = 0; i < 2 * len; i++) {
11+
const cur = nums[i]
12+
maxHeap.push(cur)
13+
sum += cur
14+
if(maxHeap.size() > len) {
15+
const tmp = maxHeap.pop()
16+
sum -= tmp
17+
}
18+
if(maxHeap.size() === len) {
19+
pre[i] = sum
20+
}
21+
}
22+
23+
for(let i = n - 1, sum = 0; i >= len; i--) {
24+
const cur = nums[i]
25+
minHeap.push(cur)
26+
sum += cur
27+
if(minHeap.size() > len) {
28+
const tmp = minHeap.pop()
29+
sum -= tmp
30+
}
31+
if(minHeap.size() === len) {
32+
suffix[i] = sum
33+
}
34+
}
35+
36+
// console.log(pre, suffix)
37+
let res = Infinity
38+
for(let i = len - 1; i < n - len; i++) {
39+
res = Math.min(res, pre[i] - suffix[i + 1])
40+
}
41+
return res
42+
};
43+
44+
class PriorityQueue {
45+
constructor(comparator = (a, b) => a > b) {
46+
this.heap = []
47+
this.top = 0
48+
this.comparator = comparator
49+
}
50+
size() {
51+
return this.heap.length
52+
}
53+
isEmpty() {
54+
return this.size() === 0
55+
}
56+
peek() {
57+
return this.heap[this.top]
58+
}
59+
push(...values) {
60+
values.forEach((value) => {
61+
this.heap.push(value)
62+
this.siftUp()
63+
})
64+
return this.size()
65+
}
66+
pop() {
67+
const poppedValue = this.peek()
68+
const bottom = this.size() - 1
69+
if (bottom > this.top) {
70+
this.swap(this.top, bottom)
71+
}
72+
this.heap.pop()
73+
this.siftDown()
74+
return poppedValue
75+
}
76+
replace(value) {
77+
const replacedValue = this.peek()
78+
this.heap[this.top] = value
79+
this.siftDown()
80+
return replacedValue
81+
}
82+
83+
parent = (i) => ((i + 1) >>> 1) - 1
84+
left = (i) => (i << 1) + 1
85+
right = (i) => (i + 1) << 1
86+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
87+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
88+
siftUp = () => {
89+
let node = this.size() - 1
90+
while (node > this.top && this.greater(node, this.parent(node))) {
91+
this.swap(node, this.parent(node))
92+
node = this.parent(node)
93+
}
94+
}
95+
siftDown = () => {
96+
let node = this.top
97+
while (
98+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
99+
(this.right(node) < this.size() && this.greater(this.right(node), node))
100+
) {
101+
let maxChild =
102+
this.right(node) < this.size() &&
103+
this.greater(this.right(node), this.left(node))
104+
? this.right(node)
105+
: this.left(node)
106+
this.swap(node, maxChild)
107+
node = maxChild
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)