Skip to content

Commit 2fc4688

Browse files
authored
Create Solution.java
1 parent f325658 commit 2fc4688

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int largestValsFromLabels(int[] values, int[] labels, int num_wanted, int use_limit) {
3+
class Data implements Comparable<Data> {
4+
int value, label;
5+
6+
public Data(int value, int label) {
7+
this.value = value;
8+
this.label = label;
9+
}
10+
11+
@Override
12+
public int compareTo(Data o) {
13+
return Integer.compare(o.value, this.value);
14+
}
15+
}
16+
int n = values.length;
17+
Data[] ds = new Data[n];
18+
for (int i = 0; i < n; ++i) {
19+
ds[i] = new Data(values[i], labels[i]);
20+
}
21+
Arrays.sort(ds);
22+
int[] map = new int[20001];
23+
int res = 0;
24+
for (int i = 0; i < n && num_wanted != 0; ++i) {
25+
if (++map[ds[i].label] <= use_limit) {
26+
res += ds[i].value;
27+
--num_wanted;
28+
}
29+
}
30+
return res;
31+
}
32+
}

0 commit comments

Comments
 (0)