-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3510-minimum-pair-removal-to-sort-array-ii.js
More file actions
156 lines (127 loc) · 4.13 KB
/
3510-minimum-pair-removal-to-sort-array-ii.js
File metadata and controls
156 lines (127 loc) · 4.13 KB
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
/**
* Minimum Pair Removal to Sort Array II
* Time Complexity: O(N log N)
* Space Complexity: O(N)
*/
var minimumPairRemoval = function (nums) {
const n = nums.length;
let ans = 0;
let inversionsCount = 0;
for (let i = 0; i < n - 1; i++) {
if (nums[i + 1] < nums[i]) {
inversionsCount++;
}
}
const nextIndices = new Int32Array(n);
const prevIndices = new Int32Array(n);
for (let i = 0; i < n; i++) {
nextIndices[i] = i + 1;
prevIndices[i] = i - 1;
}
const versions = new Int32Array(n).fill(0);
const heap = new PairRemovalMinHeap();
for (let i = 0; i < n - 1; i++) {
heap.push({
val: nums[i] + nums[i + 1],
idx: i,
ver: 0
});
}
while (inversionsCount > 0) {
const smallestPair = heap.pop();
if (!smallestPair) break;
const { val: pairSum, idx: currIndex, ver } = smallestPair;
if (ver !== versions[currIndex]) {
continue;
}
ans++;
const nextIndex = nextIndices[currIndex];
const prevIndex = prevIndices[currIndex];
if (prevIndex >= 0) {
const newPairSum = nums[prevIndex] + pairSum;
versions[prevIndex]++;
heap.push({ val: newPairSum, idx: prevIndex, ver: versions[prevIndex] });
if (nums[prevIndex] > nums[currIndex]) inversionsCount--;
if (nums[prevIndex] > pairSum) inversionsCount++;
}
if (nums[nextIndex] < nums[currIndex]) {
inversionsCount--;
}
const nextNextIndex = nextIndex < n ? nextIndices[nextIndex] : n;
if (nextNextIndex < n) {
const newPairSum = pairSum + nums[nextNextIndex];
versions[nextIndex]++;
versions[currIndex]++;
heap.push({ val: newPairSum, idx: currIndex, ver: versions[currIndex] });
if (nums[nextNextIndex] < nums[nextIndex]) inversionsCount--;
if (nums[nextNextIndex] < pairSum) inversionsCount++;
prevIndices[nextNextIndex] = currIndex;
} else {
versions[currIndex]++;
}
nextIndices[currIndex] = nextNextIndex;
nums[currIndex] = pairSum;
}
return ans;
};
class PairRemovalMinHeap {
constructor() {
this.heap = [];
}
compare(a, b) {
if (a.val !== b.val) {
return a.val - b.val;
}
return a.idx - b.idx;
}
push(val) {
this.heap.push(val);
this.bubbleUp(this.heap.length - 1);
}
pop() {
if (this.heap.length === 0) return null;
const min = this.heap[0];
const end = this.heap.pop();
if (this.heap.length > 0) {
this.heap[0] = end;
this.bubbleDown(0);
}
return min;
}
bubbleUp(index) {
const element = this.heap[index];
while (index > 0) {
const parentIdx = Math.floor((index - 1) / 2);
const parent = this.heap[parentIdx];
if (this.compare(element, parent) >= 0) break;
this.heap[index] = parent;
this.heap[parentIdx] = element;
index = parentIdx;
}
}
bubbleDown(index) {
const length = this.heap.length;
const element = this.heap[index];
while (true) {
let leftChildIdx = 2 * index + 1;
let rightChildIdx = 2 * index + 2;
let swapIdx = null;
if (leftChildIdx < length) {
if (this.compare(this.heap[leftChildIdx], element) < 0) {
swapIdx = leftChildIdx;
}
}
if (rightChildIdx < length) {
const rightChild = this.heap[rightChildIdx];
const currentBest = swapIdx === null ? element : this.heap[swapIdx];
if (this.compare(rightChild, currentBest) < 0) {
swapIdx = rightChildIdx;
}
}
if (swapIdx === null) break;
this.heap[index] = this.heap[swapIdx];
this.heap[swapIdx] = element;
index = swapIdx;
}
}
};