Skip to content

Commit 3b02c52

Browse files
committed
update Top-K(add heap solution)
1 parent 08e2d4d commit 3b02c52

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

Classical algorithm problem/KMP.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
while (j > -1 && str[j + 1] !== str[i]) {
1414
j = next[j]
1515
}
16-
if (str[j + 1] === str[i]) {
16+
if (str[i] === str[j + 1]) {
1717
j++
1818
}
1919
next[i] = j
2020
}
21-
};
21+
}
2222

2323
function kmp(str, pstr) {
2424
let slen = str.length,
2525
plen = pstr.length,
26-
i = 0,
2726
j = -1,
2827
next = new Array(plen),
2928
res = [];
@@ -35,15 +34,15 @@
3534
if (str[i] === pstr[j + 1]) {
3635
j++
3736
}
38-
if(j === plen - 1){
37+
if (j === plen - 1) {
3938
res.push(i - j);
4039
j = -1
4140
}
4241
}
4342
return res.length ? res : 0
4443
}
4544

46-
console.log(kmp("bacbababadababacambabacaddababacasdsd", "ababaca"))
45+
console.log(kmp("bbc abcdab abcdababacde", "ababac"))
4746
</script>
4847
</body>
4948
</html>

Classical algorithm problem/Top K.html

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@
1010
* 以求top k大为例
1111
* @param arr
1212
*/
13-
//region bf解法
1413
function topK(arr, k) {
15-
let index = bfprt(arr, 0, arr.length - 1, k),
16-
res = arr.slice(0, index + 1);
17-
console.log(`原数组:${arr}`);
18-
console.log(`前${k}大元素:${res}`)
14+
let bfArr = arr.slice(0),
15+
heapArr = arr.slice(0);
16+
//bf solution
17+
let bfindex = bfprt(arr, 0, bfArr.length - 1, k),
18+
ret1 = arr.slice(0, bfindex + 1);
19+
20+
console.log(`bf solution: 前${k}大元素:${ret1}`);
21+
22+
//heap solution
23+
let heap = new indexMaxHeap(heapArr),
24+
ret2 = [];
25+
for (let i = 0; i < k; i++) {
26+
ret2.push(heap.extractMax())
27+
}
28+
console.log(`heap solution: 前${k}大元素:${ret2}`)
29+
1930
}
2031

32+
//region bf solution
2133
let bfprt = function (arr, l, r, k) {
2234
let central_index = getCentralIndex(arr, l, r),
2335
boundary_index = partition(arr, l, r, central_index),
@@ -75,6 +87,67 @@
7587
};
7688
//endregion
7789

90+
//region heap solution
91+
class indexMaxHeap {
92+
constructor(arr) {
93+
this.data = [];
94+
this.indexes = [];
95+
this.reverse = [];
96+
this.count = 0;
97+
for (let i = 0; i < arr.length; i++) {
98+
this.insert(i, arr[i])
99+
}
100+
101+
}
102+
103+
insert(i, value) {
104+
this.data[++i] = value;
105+
this.indexes[++this.count] = i;
106+
this.reverse[i] = this.count;
107+
this.shiftUp(this.count)
108+
109+
}
110+
111+
shiftUp(k) {
112+
while (k > 1 && this.data[this.indexes[k]] > this.data[this.indexes[k >> 1]]) {
113+
this.swapIndex(k, k >> 1)
114+
k = k >> 1
115+
}
116+
}
117+
118+
shiftDown(k) {
119+
while (2 * k <= this.count) {
120+
let j = 2 * k;
121+
if (j + 1 <= this.count - 1 && this.data[this.indexes[j + 1]] > this.data[this.indexes[j]]) {
122+
j += 1;
123+
}
124+
if (this.data[this.indexes[k]] >= this.data[this.indexes[j]]) return;
125+
126+
this.swapIndex(k, j)
127+
k = j;
128+
}
129+
}
130+
131+
extractMax() {
132+
let res = this.data[this.indexes[1]];
133+
this.swapIndex(1, this.count);
134+
this.reverse[this.indexes[this.count]] = 0;
135+
this.count--;
136+
this.shiftDown(1);
137+
return res
138+
}
139+
140+
swapIndex(i, j) {
141+
[this.indexes[i], this.indexes[j]] = [this.indexes[j], this.indexes[i]]
142+
143+
this.reverse[this.indexes[i]] = i;
144+
this.reverse[this.indexes[j]] = j
145+
146+
}
147+
}
148+
149+
//endregion
150+
78151
topK([32, 153, 100, -50, -10, 6, 5, 1356, 20, 160, 2, 1432, 4, 50, 14, 102, -30, 3, 45, 1312, 1, -1, -3], 7)
79152
</script>
80153
</body>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy