-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1090.java
More file actions
105 lines (102 loc) · 3.35 KB
/
Copy path1090.java
File metadata and controls
105 lines (102 loc) · 3.35 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 1090. Largest Values From Labels
//
// There is a set of n items. You are given two integer arrays values and labels where the value and the label of the ith element are values[i] and labels[i] respectively. You are also given two integers numWanted and useLimit.
//
// Choose a subset s of the n elements such that:
//
// The size of the subset s is less than or equal to numWanted.
// There are at most useLimit items with the same label in s.
// The score of a subset is the sum of the values in the subset.
//
// Return the maximum score of a subset s.
//
//
//
// Example 1:
//
// Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1
// Output: 9
// Explanation: The subset chosen is the first, third, and fifth items.
// Example 2:
//
// Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2
// Output: 12
// Explanation: The subset chosen is the first, second, and third items.
// Example 3:
//
// Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1
// Output: 16
// Explanation: The subset chosen is the first and fourth items.
//
//
// Constraints:
//
// n == values.length == labels.length
// 1 <= n <= 2 * 104
// 0 <= values[i], labels[i] <= 2 * 104
// 1 <= numWanted, useLimit <= n
//
// Runtime 17ms Beats 75.89%of users with Java
// Memory 46.89MB Beats 19.86%of users with Java
class Solution {
public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
int ans = 0;
Map<Integer, Integer> labelMap = new HashMap<>();
Value[] vArr = new Value[values.length];
for (int i = 0; i < vArr.length; i++) {
vArr[i] = new Value(values[i], labels[i]);
}
Arrays.sort(vArr, (a, b) -> b.value - a.value);
int count = 0;
for (int i = 0; i < vArr.length; i++) {
int usage = labelMap.getOrDefault(vArr[i].label, 0);
if (usage < useLimit) {
ans = ans + vArr[i].value;
labelMap.put(vArr[i].label, usage + 1);
count++;
if (count == numWanted) break;
}
}
return ans;
}
class Value {
int value;
int label;
public Value(int value, int label) {
this.value = value;
this.label = label;
}
}
}
// Runtime 15ms Beats 95.04%of users with Java
// Memory 46.24MB Beats 57.45%of users with Java
class Solution {
public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
int ans = 0;
Map<Integer, Integer> labelMap = new HashMap<>();
PriorityQueue<Value> pq = new PriorityQueue<>((a, b) -> b.value - a.value);
for (int i = 0; i < values.length; i++) {
pq.add(new Value(values[i], labels[i]));
}
int count = 0;
while (pq.size() > 0) {
Value top = pq.remove();
int usage = labelMap.getOrDefault(top.label, 0);
if (usage < useLimit) {
ans = ans + top.value;
labelMap.put(top.label, usage + 1);
count++;
if (count == numWanted) break;
}
}
return ans;
}
class Value {
int value;
int label;
public Value(int value, int label) {
this.value = value;
this.label = label;
}
}
}