Skip to content

Commit 674d430

Browse files
committed
Add solution #1090
1 parent fba9ecc commit 674d430

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,129 LeetCode solutions in JavaScript
1+
# 1,130 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -870,6 +870,7 @@
870870
1080|[Insufficient Nodes in Root to Leaf Paths](./solutions/1080-insufficient-nodes-in-root-to-leaf-paths.js)|Medium|
871871
1081|[Smallest Subsequence of Distinct Characters](./solutions/1081-smallest-subsequence-of-distinct-characters.js)|Medium|
872872
1089|[Duplicate Zeros](./solutions/1089-duplicate-zeros.js)|Easy|
873+
1090|[Largest Values From Labels](./solutions/1090-largest-values-from-labels.js)|Medium|
873874
1092|[Shortest Common Supersequence](./solutions/1092-shortest-common-supersequence.js)|Hard|
874875
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
875876
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 1090. Largest Values From Labels
3+
* https://leetcode.com/problems/largest-values-from-labels/
4+
* Difficulty: Medium
5+
*
6+
* You are given n item's value and label as two integer arrays values and labels. You are also
7+
* given two integers numWanted and useLimit.
8+
*
9+
* Your task is to find a subset of items with the maximum sum of their values such that:
10+
* - The number of items is at most numWanted.
11+
* - The number of items with the same label is at most useLimit.
12+
*
13+
* Return the maximum sum.
14+
*/
15+
16+
/**
17+
* @param {number[]} values
18+
* @param {number[]} labels
19+
* @param {number} numWanted
20+
* @param {number} useLimit
21+
* @return {number}
22+
*/
23+
var largestValsFromLabels = function(values, labels, numWanted, useLimit) {
24+
const items = values.map((value, index) => ({ value, label: labels[index] }));
25+
items.sort((a, b) => b.value - a.value);
26+
27+
const labelCount = new Map();
28+
let result = 0;
29+
let itemsUsed = 0;
30+
31+
for (const { value, label } of items) {
32+
const currentCount = labelCount.get(label) || 0;
33+
if (itemsUsed < numWanted && currentCount < useLimit) {
34+
result += value;
35+
labelCount.set(label, currentCount + 1);
36+
itemsUsed++;
37+
}
38+
}
39+
40+
return result;
41+
};

0 commit comments

Comments
 (0)