forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1090.java
36 lines (34 loc) · 1.21 KB
/
_1090.java
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
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class _1090 {
public static class Solution1 {
public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
int[][] tuple = new int[values.length][2];
for (int i = 0; i < values.length; i++) {
tuple[i][0] = values[i];
tuple[i][1] = labels[i];
}
Arrays.sort(tuple, (a, b) -> b[0] - a[0]);
Map<Integer, Integer> labelUsedCountMap = new HashMap<>();
int sum = 0;
int numbersUsed = 0;
for (int i = 0; i < values.length; i++) {
int val = tuple[i][0];
int usedCount = labelUsedCountMap.getOrDefault(tuple[i][1], 0);
if (usedCount >= useLimit) {
continue;
} else {
sum += val;
numbersUsed++;
labelUsedCountMap.put(tuple[i][1], usedCount + 1);
}
if (numbersUsed >= numWanted) {
break;
}
}
return sum;
}
}
}