Skip to content

Commit 4049c90

Browse files
authored
Create 1090-largest-values-from-labels.js
1 parent 3df8591 commit 4049c90

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1090-largest-values-from-labels.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} values
3+
* @param {number[]} labels
4+
* @param {number} num_wanted
5+
* @param {number} use_limit
6+
* @return {number}
7+
*/
8+
const largestValsFromLabels = function(values, labels, num_wanted, use_limit) {
9+
return Object.entries(
10+
labels.reduce((ret, l, i) => {
11+
ret[l] = (ret[l] || []).concat(values[i])
12+
return ret
13+
}, {})
14+
)
15+
.reduce(
16+
(candi, [k, vals]) =>
17+
candi.concat(vals.sort((a, b) => b - a).slice(0, use_limit)),
18+
[]
19+
)
20+
.sort((a, b) => b - a)
21+
.slice(0, num_wanted)
22+
.reduce((ret, n) => ret + n, 0)
23+
};

0 commit comments

Comments
 (0)