-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsolution1.js
More file actions
36 lines (33 loc) · 787 Bytes
/
solution1.js
File metadata and controls
36 lines (33 loc) · 787 Bytes
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
/**
* https://leetcode-cn.com/problems/reduce-array-size-to-the-half/
*
* 1338. 数组大小减半
*
* Medium
*
* 132ms 92.68%
* 66.6mb 100.00%
*
* 考察点:
* - 哈希表
*/
const minSetSize = arr => {
const record = new Map();
for (let i = 0, max = arr.length; i < max; i++) {
if (!record.has(arr[i])) {
record.set(arr[i], 0);
}
record.set(arr[i], record.get(arr[i]) + 1);
}
const sortedCount = [...record.values()].sort((a, b) => b - a);
let halfLength = Math.floor(arr.length / 2);
let currentTotalCount = 0;
for (let i = 0; i < sortedCount.length; i++) {
if (currentTotalCount + sortedCount[i] < halfLength) {
currentTotalCount += sortedCount[i];
} else {
return i + 1;
}
}
return sortedCount.length;
}