-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityQueue.js
53 lines (45 loc) · 1.1 KB
/
priorityQueue.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
const {
PriorityQueue,
MaxPriorityQueue,
} = require('@datastructures-js/priority-queue');
var minSetSize = function (arr) {
let map = new Map();
const totalLength = arr.length;
let length = totalLength;
let ans = 0;
for (let i = 0; i < arr.length; i++) {
if (map.has(arr[i])) {
map.set(arr[i], map.get(arr[i]) + 1);
} else {
map.set(arr[i], 1);
}
}
debugger;
// const maxPQ = new PriorityQueue((a, b) => {
// if (a.value > b.value) {
// return -1;
// } else {
// return 1;
// }
// });
// for (const [key, value] of map) {
// maxPQ.enqueue({ key: key, value: value });
// }
// while (length >= Math.floor(totalLength / 2)) {
// const count = maxPQ.dequeue();
// length = length - count.value;
// ans++;
// }
const maxPQ = new MaxPriorityQueue();
for (const [key, value] of map) {
maxPQ.enqueue(value);
}
while (length >= Math.floor(totalLength / 2)) {
const count = maxPQ.dequeue();
length = length - count;
ans++;
}
console.log(ans);
return ans;
};
minSetSize([3, 3, 3, 3, 5, 5, 5, 2, 2, 7]);