We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c526bb0 commit bf38260Copy full SHA for bf38260
1338.数组大小减半.js
@@ -0,0 +1,33 @@
1
+/**
2
+ * @param {number[]} arr
3
+ * @return {number}
4
+ */
5
+var minSetSize = function(arr) {
6
+ const map = {};
7
+ for (const n of arr) {
8
+ map[n] = map[n] || 0;
9
+ map[n]++;
10
+ }
11
+
12
+ let sum = 0;
13
+ const list = Object.keys(map).map(n => {
14
+ sum += map[n];
15
+ return {
16
+ n,
17
+ c: map[n]
18
19
+ })
20
21
+ list.sort((a, b) => b.c - a.c)
22
23
+ let result = 0;
24
+ let current = sum;
25
+ for (const a of list) {
26
+ result++;
27
+ current -= a.c;
28
+ if (current * 2 <= sum) {
29
+ break;
30
31
32
+ return result;
33
+};
0 commit comments