Skip to content

Commit 66ee459

Browse files
Min heap
1 parent 84b1a02 commit 66ee459

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed

FE-JS-Questions/eventEmitter.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Implement an EventEmitter
2+
3+
```js
4+
const emitter = new EventEmitter();
5+
const sub = emitter.subscribe(eventName, callback); // typeof eventName === string
6+
emitter.emit(eventName, a, b, c, d, ...);
7+
sub.unsubscribe();
8+
```
9+
10+
```js
11+
class EventEmitter {
12+
constructor() {
13+
this.events = new Map();
14+
}
15+
16+
subscribe(eventName, callback) {
17+
this.events.set(eventName, callback);
18+
19+
return {
20+
unsubscribe : () => this.events.delete(eventName)
21+
};
22+
}
23+
24+
emit(eventName, ...args) {
25+
if (this.events.has(eventName)) {
26+
this.events.get(eventName).apply(null, args);
27+
}
28+
}
29+
}
30+
```
31+
32+
33+
```js
34+
// Example:
35+
function emitSuccess() {
36+
console.log("Emitter class implemented");
37+
}
38+
const emitter = new EventEmitter();
39+
const sub = emitter.subscribe("change", emitSuccess);
40+
emitter.emit("change", 1,2,3,4);
41+
sub.unsubscribe();
42+
```

concepts/MinHeap.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Implementation of MinHeap in JS
2+
3+
> Formulae for indices
4+
5+
1. Parent : idx / 2 (where idx is the current index)
6+
2. Left : idx * 2;
7+
3. Right: idx * 2 + 1;
8+
9+
```js
10+
class MinHeap {
11+
constructor() {
12+
this.heap = [null];
13+
}
14+
15+
insert(val) {
16+
this.heap.push(val);
17+
if (this.heap.length > 2) {
18+
let idx = this.heap.length - 1;
19+
20+
while (this.heap[idx] < this.heap[Math.floor(idx / 2)]) {
21+
if (idx >= 1) {
22+
[this.heap[Math.floor(idx / 2)], this.heap[idx]] =
23+
[this.heap[idx], this.heap[Math.floor(idx / 2)]];
24+
25+
if (Math.floor(idx / 2) > 1) {
26+
idx = Math.floor(idx / 2);
27+
} else {
28+
break;
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
remove() {
36+
let smallest = this.heap[1];
37+
38+
if (this.heap.length > 2) {
39+
this.heap[1] = this.heap[this.heap.length - 1];
40+
this.heap.splice(this.heap.length - 1);
41+
42+
if (this.heap.length === 3) {
43+
if (this.heap[1] > this.heap[2]) {
44+
[this.heap[1], this.heap[2]] = [this.heap[2], this.heap[1]];
45+
}
46+
return smallest;
47+
}
48+
let i = 1;
49+
let left = 2 * i;
50+
let right = (2 * 1) + 1;
51+
52+
while (this.heap[i] >= this.heap[left] || this.heap[i] >= this.heap[right]) {
53+
if (this.heap[left] < this.heap[right]) {
54+
[this.heap[i], this.heap[left]] = [this.heap[left], this.heap[i]];
55+
i = 2 * i;
56+
} else {
57+
[this.heap[i], this.heap[right]] = [this.heap[right], this.heap[i]];
58+
i = (2 * i) + 1;
59+
}
60+
left = 2 * i;
61+
right = (2 * i) + 1;
62+
63+
if (this.heap[left] == undefined || this.heap[right] == undefined) {
64+
break;
65+
}
66+
}
67+
} else if (this.heap.length === 2) {
68+
this.heap.splice(1, 1);
69+
} else {
70+
return null;
71+
}
72+
73+
return smallest;
74+
}
75+
76+
size() {
77+
return this.heap.length;
78+
}
79+
}
80+
```
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Find the kth largest element in an unsorted array.
3+
* Note that it is the kth largest element in the sorted order, not the kth distinct element.
4+
5+
Example 1:
6+
7+
Input: [3,2,1,5,6,4] and k = 2
8+
Output: 5
9+
Example 2:
10+
11+
Input: [3,2,3,1,2,4,5,5,6] and k = 4
12+
Output: 4
13+
Note:
14+
You may assume k is always valid, 1 ≤ k ≤ array's length.
15+
*/
16+
17+
// Solving with min heap -> Will build MinHeap in JavaScript first
18+
19+
class MinHeap {
20+
constructor() {
21+
this.heap = [null];
22+
}
23+
24+
insert(value) {
25+
this.heap.push(value);
26+
if (this.heap.length > 2) {
27+
let idx = this.heap.length - 1; // Get the index of the last element of heap
28+
29+
while (this.heap[idx] < this.heap[Math.floor(idx / 2)]) {
30+
if (idx >= 1) {
31+
[this.heap[Math.floor(idx / 2)], this.heap[idx]] = [this.heap[idx], this.heap[Math.floor(idx / 2)]];
32+
33+
if (Math.floor(idx / 2) > 1) {
34+
idx = Math.floor(idx / 2);
35+
} else {
36+
break;
37+
}
38+
}
39+
}
40+
}
41+
}
42+
43+
remove() {
44+
let smallest = this.heap[1];
45+
46+
if (this.heap.length > 2) {
47+
this.heap[1] = this.heap[this.heap.length - 1];
48+
this.heap.splice(this.heap.length - 1);
49+
50+
if (this.heap.length === 3) {
51+
if (this.heap[1] > this.heap[2]) {
52+
[this.heap[1], this.heap[2]] = [this.heap[2], this.heap[1]];
53+
}
54+
return smallest;
55+
}
56+
57+
let i = 1;
58+
let left = 2 * i;
59+
let right = (2 * i) + 1;
60+
61+
while (this.heap[i] >= this.heap[left] || this.heap[i] >= this.heap[right]) {
62+
if (this.heap[left] < this.heap[right]) {
63+
[this.heap[i], this.heap[left]] = [this.heap[left], this.heap[i]];
64+
i = 2 * i;
65+
} else {
66+
[this.heap[i], this.heap[right]] = [this.heap[right], this.heap[i]];
67+
i = (2 * i) + 1;
68+
}
69+
left = 2 * i;
70+
right = (2 * i) + 1;
71+
if (this.heap[left] == undefined || this.heap[right] == undefined) {
72+
break;
73+
}
74+
}
75+
} else if (this.heap.length === 2) {
76+
this.heap.splice(1, 1);
77+
} else {
78+
return null;
79+
}
80+
81+
return smallest;
82+
}
83+
84+
sort() {
85+
const result = [];
86+
87+
while(this.heap.length > 1) {
88+
result.push(this.remove());
89+
}
90+
91+
return result;
92+
}
93+
94+
size() {
95+
return this.heap.length;
96+
}
97+
}
98+
99+
const findKthLargest = (nums, k) => {
100+
const minHeap = new MinHeap();
101+
102+
for (let num of nums) {
103+
minHeap.insert(num);
104+
105+
if (minHeap.size() > k + 1) {
106+
minHeap.remove();
107+
}
108+
}
109+
110+
return minHeap.remove();
111+
}

0 commit comments

Comments
 (0)